013 LISTS Unlocked1
013 LISTS Unlocked1
• 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 of lists are :
•[] # empty list
• [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,90000.5] # list of mixed value
• [„A‟, „B‟, „C‟] # list of 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
• L = list()
• LONG LIST
• L = [1,2,3,44,55,66,77,88,99,4,3,5,6,7,88,100,300
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 and to print 200 we
have to write L[3][1] i.e. of 3rd index print 2nd value
• 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‟)
• L1 = list(T)
• >>>L2
• [„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 be entered”))
>>> print(“list is “, list1)
• eval(„5+10‟) 15
• 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”,”cherry”]
• In above list items from mango to cherry are 0 to 4 and from cherry to
mango will be -1 to -5
0 1 2 3 4
Mango Apple Guaua Pomegranate cherry
-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.
• List elements are accessed just like string like str[2] means
character at 2nd index List1[2] means elements at 2nd index and
List1[1:3] means all items between index 1 to 2
• Another similarities are :
Length : the same len() function we can use on list to find out number of
elements in the list
Indexing and Slicing
Membership operators (in and not in)
Concatenation and replication operators + and *
val = [10,20,30,50,100]
for i in val:
print(i)
Program to print list elements along with their index (both +ve,-ve)
val = [10,20,30,50,100]
length = len(val)
for i in range(length):
print ("At Index ", i," and index ",i-length, 'is :', val[i])
• Python allows us to use all the relational operators on list like ==, >=,
<=,!=,>,<
• Python will compare individual elements of each list.
>>> L1=[1,3,5]
>>> L2=[1,3,5]
>>> L3=[1,5,3]
>>> L1==L2
True
>>> L1==L3
False
>>> L1<L2
False
>>> L1<L3
True
>>> L4=[1,3,6]
>>>L1<L4
True
Comparison Result Reason
[1,2,8,9] <[9,1] True 1<9
[1,2,8,9]<[1,2,9,1] True 8<9
[1,2,8,9]<[1,2,9,10] True 8<9
[1,2,8,9]<[1,2,8,4] False 9<4 is false
• Joining List
• Jointing the 2 list is very easy, we can use (+) to join 2 or
more list
• Fruits=[“apple”,”mango”,”grapes”]
• Veg=[“spinach”,”carrot”,”potato”]
• Fveg = Fruits + Veg
• Fveg # [“apple”,”mango”,”grapes”,
“spinach”,”carrot”,”potato”]
• Remember: you can only add list with another list not
with int, float, complex, or string type.
• Repeating of Replicating List
• Repeating any list is very easy, we can use (*)
• Fruits=[“apple”,”mango”,”grapes”]
• Fruits*2 #[“apple”,”mango”,”grapes”,“apple”,”mango”,”grapes”]
• String slicing
• Listname[start:End] # from start index to end-1 index
val = [10,20,30,40,1,2,3,100,200]
print(val[0:3])
print(val[3:8])
print(val[-4:-1])
val2 = val[2:-2]
print(val2)
print(val2[0:])
print(val[::-1])
val[5]=101
print(val[-8:-2])
val = [10,20,30,40,1,2,3,100,200]
print(val[3:50])
print(val[-20:4])
print(val[10:20])
sum=0
for x in slice2:
print(x,"==",end='')
sum+=x
avg = sum / len(slice2)
print("Average of slice 2 =",avg)
items=["One","Two","Three","Four"]
items[0:2]=[1,2]
for i in items:
print(i,end=' ')
items[0:3]="Fantastic"
print()
for i in items:
print(i,end=' ')
>>> items=[1,2,3,4]
>>> items[3:]=“hello”
>>> items # [1,2,3,‟h‟,‟e‟,‟l‟,‟l‟,‟o‟], it will work because
string is also a sequence
But if you try to assign as:
items[3:] = 100 # Error
If we pass index which is more than highest range, then Python will
use next position after the highest index for e.g.
>>> items[100:200]=[111,222]
Python will append these values to the end of list
>>>items [1,2,3,‟h‟,‟e‟,‟l‟,‟l‟,‟o‟,111,222]
• So far we have worked with How the Lists are declared, How to
access individual elements of List, How to perform Slicing and so
on, Now we are going to deal with basic operation on list like :
Appending, Updating, Deleting items from List
Items = [10,20,30,40]
Items[3] = 100
Items [10,20,30,100]
• Deleting items from a List: to delete items from list we will use
del statement. To delete single item give index, and to delete
multiple items use slicing
• Deleting single elements of a list
Items = [10,20,30,40,50,60,70]
del items[2]
Items [10,20,40,50,60,70]
>>> m1=[1,2,3,4]
>>> m2=[100,200]
>>> m1.append(5)
>>> m1
[1, 2, 3, 4, 5]
>>> m1.append(6,7)
>>> 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,50]
>>> L1
[10, 20, 30, 40, 50]
>>> L1.insert(0,5) #beginning
>>> L1
[5,10, 20, 30, 40, 50]
>>> L1.insert(len(L1),100) #last
>>> L1
[5,10, 20, 30, 40, 50,100]
>>> L1.insert(-10,2)
>>> L1
[2,5,10, 20, 30, 40, 50,100]
• We have read about this function earlier, it is used to remove item
from list.
ListObject.pop([index]) #if index is not passed last item 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()