Escolar Documentos
Profissional Documentos
Cultura Documentos
List is a standard data type of Python that can store a sequence of values belonging to any
type. List is mutable sequence i.e element can be changed in place.
Example
List=[1,2,3,4,5]
List1=['p','r','o','b','l','e','m']
List2=['pan','ran','oggi','blade','lemon','egg','mango']
Creating List
List can be created by assigning a variable with the values enclosed in square bracket
separated by comma.
Eg List=[1,2,3,4,5]
List can contain values of mixed data types. List are of different types:
1. Empty List: List with no item is a empty list. Eg List=[ ]. It can be created with the
function list1=list().it generates the empty list with the name list1. This list is
equivalent to 0 and has truth value false.
2. Long List: List with many items.eg list1= [11,12,13,14,15,16
………….17,18,19,20]
3. Nested List: List can have list as its element. eg List1= [11,12,[13,14],15]. List1 is
nested list with four elements: 11,12,[13,14] and 15. List1[2] element is a list [13,14].
Length of List1 is 4.
Creating List from existing sequence
List1=list(sequence)
Example: List1=list(‘Computer’) >>>List1 >>[’C’,’o’,’m’,’p’,’u’,’t’,’e’,’r’]
Accessing List
List are accessed as string with the index starting from 0 as forward indexing.
Forward Indexing 0 1 2 3 4 5
P y T h o n
-6 -5 -4 -3 -2 -1 Backward Indexing
List Operations
1. Joining Lists: Two Lists can be joined through addition.
>>>l1=[1,2,3]
>>>l2=[4,5,6]
>>>l3=l1+l2
>>>l3
[1,2,3,4,5,6]
2. Repeating or Replicating Lists: Multiply(*) operator replicates the List specified number of
times
>>>l1=[1,2,3] >>>l1*3 [1,2,3,1,2,3,1,2,3]
3. Slicing the List: List slices are the subpart of a list extracted out.List slices can be created
through the use of indexes.
Seq=List[start:stop] : creates list slice out of List1 with element falling in between indexes
start and stop not including stop.
List also supports slice steps. Example, Seq=List[start:stop:step] creates list slice out of List
with element falling in between indexes start and stop not including stop, skipping step-1
element in between.
pop() method can be used to remove single element from the list. It returns the removed
item. It is generally used when we want to store the element being deleted for later use.
>>> List=[1,2,3,4,5,6,7,8,9,10] >>> List.pop() 10
>>> List=[1,2,3,4,5,6,7,8,9,10] >>> List.pop(4) 5