LIST in Python
LIST in Python
accessing list
• List Operations
• Working with list
• List functions and
Methods
• Is a container that are used to store a list of values
of any type
• List is mutable type i.e. we can change value in
place without
creating a fresh list
• List is a type of sequence like tuples and string but
in differs them in the way that lists are mutable
but string and tuples are immutable
• List are created using SQUARE BRACKETS
([])
• Some
• [ ] of the examples#ofempty list :
lists are
• [1,2,3] # list of integers
• [10,20,13.75,100.5,90] # list of
integers and float
• [“red”,"green”,"blue”] # list of string
• [“E001”,"Rakesh”,1,90 # list of mixed
000.5] value # list of
• ["A‟, "B‟, "C‟] characters
• To create a list the following syntax we need
to follow:
• ListName = [ ] Or
• ListName = [ value1, value2,…..]
• For example
• Family = [“father”,"mother","bro”,”sis”]
• Student = [1,"Aman”,"XI",3150]
• The above construct is known as list display
construct
• Consider more examples
• EMPTY LIST
• L=[] Or
• LONG LIST
•L =
[1,2,3,44,55,66,77,88,99,4,3,5,6,7,88,100,30
0
12,13,14,56,78]
• NESTED LIST
• L = [1,2,4,[100,200,300], 20]
• The above code will create List L with 5
elements because it will count
[100,200,300] as one element. Now L[3] is
list of 3 elements
• To print if we write : L[1] it will display 2
• We can use the following
syntax:
• ListName = list(sequence)
• For example (with
string)
• L1 = list("welcome‟)
• >>>L1
• ["w‟,‟e‟,‟l‟,‟c‟,‟o‟,‟m‟,‟
e‟]
• With tuple
• T = ("A‟,‟B‟,‟C‟,‟D‟)
• We can also create list of single characters or
single digits through keyboard input:
For example:
>>> list1 = list(input("Enter list
elements‟))
>>> list1
From the above code whatever values we will
enter will be of STRING type. To store list of
integers through input in python we can use
eval() to convert the list items to integers
>>> list1 = eval(input(“enter list to
• eval("5+10‟) 1
5
• Y = eval(“2*5”)
• print(Y) 10
• Eval wil not only convert the values to int type but
also interpret the value as intended type i.e. if
you enter float value it will convert it to float or if
it is list or tuple it will convert it.
>>> list1 = eval(input(“Enter
values :”))
>>> enter values:
[10,20,30]
>>> list1
[10,20,30]
• Similarity with strings:
• Just like string, every individual elements of lists
are accessed from their index position which is
from 0 to length-1 in forward indexing and from -1
to –length in backward indexing.
• For example
• Fruits =
[“mango”,”apple”,"guaua”,"pomegranate","c
herry”]
• In above
0
list1 items2 from mango
3
to cherry
4
are 0 to
4 Mango
and from cherry to mango will be -1 to -5
Apple Guaua Pomegranate cherr
y
-5 -4 -3 -2 -1
• If list element is large, it will store the
reference in the list and the values
will be stored somewhere else.
• >>> student =
[1,‟Akash‟,‟XIA‟,3150]
• >>> student[3]=6300
• >>> student
• [1,‟Akash‟,‟XIA‟,6300]
• Just like String , We can use “for” loop to traverse
the list.
val = [10,20,30,50,100]
for i in val:
print(i)
Step slicing in
List
val =
[10,20,30,40,1,2,3,100,
200]
print(val[0:9:2]) #
10,30,1,3,200
val =
[10,20,30,40,1,2,3,100,200]
print(val[2:9:3])
print(val[::3])
print(val[::-2])
c
val =
[10,20,30,40,1,2,3,100,200,10,20,30,11,12,15,17,19,90,77,35]
slice1 =
val[5:15:2]
slice2 =
val[::4] sum =
0
for x in slice1:
print(x,
"==",en
d='')
sum+=
x
print("Sum of
slice1
elements are
",sum)
items=["One","Two","Three
","Four"] items[0:2]=[1,2]
for i in items:
print(i,end
=' ')
items[0:3]="Fan
tastic" print()
for i in
items:
print(
i,end=' ')
>>> items=[1,2,3,4]
>>>
>>> # [1,2,3,‟h‟,‟e‟,‟l‟,‟l‟,‟o‟], it
items[3:]=“hello”
items will work because string is
also a as:
But if you try to assign sequence
items[3:] = 100 # Error
Traceback (most
recent call last):
>>>
m2.extend([300,40
>>> m2
0])
[100, 200, 300,
400]
>>>
len(m2)
4
• This function is used to add elements to list like
append() and extend(). However both append() and
extend() insert the element at the end of the list. But
insert() allows us to add new elements anywhere in
the list i.e. at position of our choice.
ListObject.insert(Position,item)
>>> L1=[10,20,30,40,50]
>>> L1
[10, 20, 30, 40, 50]
>>> L1.insert(3,35)
>>> L1
[10, 20, 30, 35, 40, 50]
>>>
>>>
L1=[10,20,30,40,5
0]
[10, 20, 30, 40, 50]
>>> L1
>>> L1.insert(0,5) #beginn
>>> L1 ing
[5,10, 20, 30, 40,
50] #la
>>> st
L1.insert(len(L1),10
0)
>>> L1
[5,10, 20, 30, 40,
50,100]
>>> L1.insert(-
• We have read about this function earlier, it is used
to remove item from list.
ListObject.pop([i # if index is not passed last item
ndex]) will be deleted
>>>
L1=[10,20,30,40,50]
>>> L1.pop()
50
>>> val = L1.pop(2)
>>> val
30
>>> L1 [10,
20, 40]
>>>
• The pop() method raises an exception(run
time error) if
the list is already empty.
>>> L1= [ ]
>>> L1.pop()