CSC1041 Session 10 Python Collection Data Types
CSC1041 Session 10 Python Collection Data Types
Programming Laboratory I
Session 11
• list, tuple, dict and set are the 4 built-in data types in Python
that can store collection of data
List Creation
l i s t 1 = ["Kandy","Colombo","Galle","Kandy"]
print(list1)
l i s t 2 = ["Kandy",130,True,22.2]
print(list2)
l i s t 1 = ["Kand y", "C olo mbo ", "Ga lle "," Ja ffn a", "Am pa ra" ]
list1.append("Kataragama")
lis t1 .i ns er t( 2, "M at ar a" )
l i s t 2 = ["Mannar","Kataragama"]
l i s t 1 . e xt e n d ( li s t 2 )
l i s t 1 = ["Kandy "," Col omb o" ,"G all e", "J aff na" ," Amp ara "]
l i s t 1 . r e m o v e (" G a l l e " )
list 1. pop ()
de l l i s t [ 0 ]
l i s t 1 . clear( )
l i s t 1 = [ " K a n d y " , " C o l o m b o " , " G a l l e " ," J a f f n a ", " A m p a ra " ]
# Method 1
for i in list1:
pr i nt ( i )
• Using range() and len() we can access the indexes of the list
# Method 2
f o r i i n range( l e n ( l i s t 1 ) ) :
print( li s t1 [ i] )
• List comprehension offers the shortest syntax to retrieve items from list
# Method 3
[ print(i) for i in list1 ]
CSC 1041 Department of Statistics and Computer Science , University of Peradeniya. 7
List Cont…
• Write a program using a for loop to reverse the given list
l i s t 1 = ["N ", "O " ," H" ," T" ," Y ", "P "]
list2 = []
for i i n range( l e n ( l i s t 1 ) ) :
list2.insert( i , list1[-1] )
l i s t1 . p op ( )
print(list2)
Tuple Creation
• If declaring a tuple with one value, remember to add the comma (,)
tupl e1 += t upl e2
pri nt (tu pl e1 )
for i i n tuple1:
print(i)
• Similarly, we can use range() and len() to refer items using the index
• We could also use a while loop
• A set is unordered
• A set is unindexed
• A set is unchangeable (can add items but cannot change existing items)
Set Creation
set1 = { 1 2 0 , 5 0 . 4 , " S r i Lanka",True}
pri nt (se t1 )
f o r x i n set1:
print(x)
• We can use other collection data types (tuple, list, etc) to update
• Can use pop() to delete items but since it removes the last element, the
deleted element is not predictable because a set is unordered.
• We can use union() method to create a new set with items in both sets
set1 = { 1 2 0 , 5 0 . 4 , " S ri Lanka",True}
set2 = {"India",False,100}
set3 = set 1. un io n( se t2 )
pri nt (se t3 )
• We can use intersection() to return a new set that contain items available
in both sets only
set1 = { 1 2 0 , 5 0 . 4 , " S ri Lanka",True}
set2 = { " I n d i a " , F a l s e , 1 0 0 , 5 0 . 4 , " S r i Lanka"}
set3 = s e t 1 . i n t e r s e c t i o n ( s e t 2 )
Dictionary Creation
dict1 = {
" c o u n t r y " : " S r i Lanka" ,
" c i t y " : " Perade niya" ,
"reg ion ": "Central"
}
p r i n t (d i c t1 [ "r e gi o n "] )
dict1 = {
" c o u n t r y " : " S r i Lanka" ,
" c i t y " : " Perade niya“ ,
" c i t y " : " Kandy" ,
"reg ion ": "Central"
}
p r i n t ( d ic t 1 )
dict1 = {
" c o u n t r y " : " S r i Lanka" ,
" c i t y " : " Perade niya" ,
" language s" : [ " S i n h a l e s e " , " T a m i l " , " E n g l i s h " ] ,
"reg ion ”:"Central"
}
• keys() will get the list of keys in the dictionary and values() will get the list
of values
dict_keys = d i c t 1 . k e y s ( )
dict _v alu es = d i c t 1 . v a l u e s ( )
dict1 = {
" c o u n t r y " : " S r i Lanka" ,
" c i t y " : " Peradeniy a" ,
"reg ion ": "Central"
}
di ct 1. pop( " count r y" )
p r i n t ( d ic t 1 )
d i c t1 . p op i te m ()
number_dict = {
"grea ter _th an_5 0": [70 ,54, 100 0,9 8],
"le ss _th an _5 0": [3 3, 48 ,12 ,7 ]
}
number_list = [ ]
p r i n t ( d ic t 1 )
Unchangeable (can
add items but
Changeable Unchangeable Changeable
cannot change
existing item)