0% found this document useful (0 votes)
24 views

lists

Uploaded by

anilperfect
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

lists

Uploaded by

anilperfect
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Lists in python

Introduction :
 List is a collection of elements which is ordered and changeable (mutable).
 Allows duplicate values of different types separated by commas and enclosed within square
brackets ([ ]).
Difference between list and string:

List String
Mutable Immutable
Element can be assigned at specified Element/character cannot be
index assigned at specified index.
Example: Example:

>>>L=[7,4,8,9] >>>str= “python”

>>>L[2]=6 #valid >>>str[2]= ‘p’ #error

Syntax:

list-name= [item-1, item-2,..........., item-n]

Example:

mylist = ["apple", "banana", "cherry"] # a list with three

items L = [ ] # an empty list

Accessing lists:

 The values stored in a list can be accessed using the slice operator ([ ] and [:])
with indexes.
 List-name[start:end:skip] will give you elements between indices start to end-1.
 The first item in the list has the index zero (0).
Example:
>>> number=[12,56,87,45,23,97,56,27]

Forward Index
0 1 2 3 4 5 6 7
12 56 87 45 23 97 56 27
-8 -7 -6 -5 -4 -3 -2 -1
Backward Index

>>> number[2]
87
>>> number[-1]
27

Traversing a LIST:

Traversing means accessing and processing each element by using for loop.

Example:
>>> day=list(input("Enter elements :"))
Enter elements : sunday
>>> for d in day:
print(d)

Output:
s
u
n
d
a
y

List Operators:

 Joining operator +
 Repetition operator *
 Slice operator [:]
 Comparison Operator <, <=, >, >=, ==, !=

 Joining Operator (+): It joins 2 or more


elements of a list.
 Example:

>>> L1=['a',56,7.8]

>>> L2=['b','&',6]

>>> L3=[67,'f','p']

>>> L1+L2+L3

['a', 56, 7.8, 'b', '&', 6, 67, 'f', 'p']

 Membership operator: it finds if a element is a part of a list or not.


 Example:
>>> L1=['a',56,7.8]
>>> 'a' in L1
True

 Repetition Operator: It replicates a list specified number of times.

Example:

>>> L1*3

['a', 56, 7.8, 'a', 56, 7.8, 'a', 56, 7.8]

>>> 3*L1

['a', 56, 7.8, 'a', 56, 7.8, 'a', 56, 7.8]

 Slice Operator:

List-name[start:end] will give you elements between indices start to end-1.

>>> number=[12,56,87,45,23,97,56,27]

>>> number[2:-2]

[87, 45, 23, 97]

List-name[start:end:step] will give you elements between indices start to end-1 with
skipping elements as per the value of step.

>>> number[1:6:2]

[56, 45, 97]

>>> number[: : -1]

[27, 56, 97, 23, 45, 87, 56, 12] #reverses the list

List modification using slice operator:

>>> number=[12,56,87,45,23,97,56,27]

>>> number[2:4]=["hello","python"]

>>> number

[12, 56, 'hello', 'python', 23, 97, 56, 27]


Note: The values being assigned must be a sequence (list, tuple or string)

Nested lists: a list inside another list is called a nested list.

Example: >>> a=[1,2,3[4,5,6]]

List functions:-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy