Lists & Dictionary
Lists & Dictionary
LISTS
● A list is a collection of values or an ordered sequence of values/items.
CREATING A LIST
● Lists can be created by putting comma-separated values/items within square
brackets.
syntax
<list_name> = [ ]
LISTS TYPES
Empty lists
lst=[]
Long lists
lst=[10,20,30,40,50] #List of integers
lst=[“VELS”,500,98.3,”s”,-10.5] #List with elements of different data types
lst=[‘N’,’O’,’V’,’E’,’M’,’B’,’E’,’R’] #List of Characters
lst=[“AI”,”IP”,”IT”,”CS”] #List of Strings
Nested lists
lst=[3,4,5,[6,7],8]
lst=[10,20,30,40,50,60,70,80,90,100]
print(lst[0])
OUTPUT:
10
print(lst[-5])
OUTPUT:
60
print(lst[10])
OUTPUT:
IndexError: List indeed out of range
list1 = ['Good','Excellent','ok']
list1[2] = 'Fine'
print(list1)
OUTPUT:
['Good','Excellent','Fine']
OPERATIONS ON LISTS
Concatenation
lst=[“pink”,”black”]
lst=lst+[“Red”]
print(lst)
OUTPUT:
[“pink”,”black”,“Red”]
Repetition
lst=[1,2,3]
print(lst*2)
OUTPUT:
[1,2,3,1,2,3]
Membership
x=’python’
print(‘o’ in x)
OUTPUT:
True
Slicing
lst=[10,20,30,40,50,60,70,80,90]
lst[3:7]
OUTPUT:
[40,50,60,70]
TRAVERSING A LIST
● It means accessing each element of a list.
lst=[‘D’,’I’,’W’,’A’,’L’,’I’]
n=len(lst)
for i in range(n):
print(lst[i])
print(“Total no.of. characters:”)
print(n)
OUTPUT:
D
I
W
A
L
I
Total no.of. Characters: 6
NESTED LISTS
● When a list appears as an element of another list, it is called a nested list
lst=[1,2,’a’,’c’,[6,7,8],4,9]
print(lst[4])
OUTPUT:
[6,7,8]
print(lst[4][1])
OUTPUT:
7
BUILT-IN FUNCTIONS
1. len()
l=[10,20,30,40]
len(l)
OUTPUT:
4
2. list()
str=”IP”
l=list(str)
print(l)
OUTPUT:
[‘I’,’P’]
3. append()
l=[10,20,30,40]
l.append(55)
print(l)
OUTPUT:
[10,20,30,40,55]
4. extend()
l=[10,20,30,40]
ls=[50,60]
l.extend(ls)
print(l)
OUTPUT:
[10,20,30,40,50,60]
5. insert()
l=[10,20,30,40]
l.insert(2,25)
print(l)
OUTPUT:
[10,20,25,30,40]
6. count()
l=[1,2,3,2,3,2,5,4,2,9]
l.count(2)
OUTPUT:
4
7. index()
l=[10,20,30,40]
l.index(30)
print(l)
OUTPUT:
2
8. remove()
l=[10,20,30,10,40]
l.remove(10)
print(l)
OUTPUT:
[20,30,10,40]
9. pop()
l=[10,20,30,10,40]
l.pop(2)
OUTPUT:
30
10. del
l=[10,20,30,10,40]
del l[3]
print(l)
OUTPUT:
[10,20,30,40]
11. reverse()
l=[10,20,30,40]
l.reverse()
print(l)
OUTPUT:
[40,30,20,10]
12. sort()
l=[13,18,20,10,18,23]
l.sort( )
print(l)
OUTPUT:
[10,13,18,18,20,23]
13. sorted()
l=[13,18,20,10,18,23]
ls=sorted(l)
print(ls)
OUTPUT:
[10,13,18,18,20,23]
14. min()
l=[10,20,30,40]
min(l)
OUTPUT:
10
15. max()
l=[10,20,30,40]
max(l)
OUTPUT:
40
16. sum()
l=[10,20,30,40]
sum(l)
OUTPUT:
100
17. clear()
i)
l=[10,20,30,40]
l.clear()
print(l)
OUTPUT:
[]
ii)
l=[10,20,30,40]
print(l.clear())
OUTPUT:
None
DICTIONARY
● A python dictionary is a mapping of unique keys to values. It is a collection of
key-value pairs.
CREATING A DICTIONARY
● A dictionary can be created by placing items(key-value pair) inside curly braces
{}, separated by a comma.
SYNTAX:
<dictionary-name>={<key1> : <value>,<key2> :<value>…..}
OUTPUT:
{‘JAN’:’January’,’JUN’:’June’,’JUL’:’July’}
Keys: JAN, JUN, JUL
Values: January, June, July
Using dict() function
d=dict()
print(d)
OUTPUT:
{}
OUTPUT:
{0:”English”,1:”Language”,2:”Math”}
d={‘R’:’Rainy’,’S’:’Summer’,’W’:’Winter’,’A’:’Autumn’}
print(d[“W”])
OUTPUT:
Winter
MEMBERSHIP OPERATOR
1. In
d={‘R’:’Rainy’,’S’:’Summer’,’W’:’Winter’,’A’:’Autumn’}
print(‘S’ in d)
OUTPUT:
True
print(’F’ in d)
OUTPUT:
False
2. not in
print(’B’ not in d)
OUTPUT:
True
OUTPUT:
{‘R’:’Red’,’G’:’Green’,’B’:’Blue’,’O’:’Orange’,’Y’:’Yellow’}
OUTPUT:
{‘R’:’Red’,’G’:’Green’,’B’:’Black’,’O’:’Orange’}
TRAVERSING A DICTIONARY
● It means accessing each element of a dictionary.
d={‘A1’:77.3,’A3’:88.1,’A5’:46.4,’A7’:65.5}
for i in d:
print(i,”:”,d[i])
OUTPUT:
A1:77.3
A3:88.1
A5:46.4
A7:65.5
OUTPUT:
3
2. dict()
t=[(‘Mar’,31),(‘Apr’:30),(’May’:31)]
d=dict(t)
print(d)
OUTPUT:
{‘Mar’:31, ‘Apr’:30, ‘May’:31}
3. keys()
d={‘L’:’Lily’,’J’:’Jasmine’,’M’:’Marigold’,’T’:’Tulip’}
print(d.keys())
OUTPUT:
dict_keys(['L', 'J', 'M', 'T'])
4. values()
d={‘L’:’Lily’,’J’:’Jasmine’,’M’:’Marigold’,’T’:’Tulip’}
print(d.values())
OUTPUT:
dict_values(['Lily', 'Jasmine', 'Marigold', 'Tulip'])
5. items()
d={1:'one',2:'two',3:'three'}
print(d.items())
OUTPUT:
dict_items([(1, 'one'), (2, 'two'), (3, 'three')])
6. get()
d={‘M’:’Monitor’,’K’:’Keyboard’,’P’:’Printer’}
print(d.get(‘K’))
OUTPUT:
Keyboard
7. update()
dict1={‘A’:’Apple’,’B’:’Banana’}
dict2={‘G’:’Grapes’,’M’:’Mango’}
dict1.update(dict2)
print(dict1)
print(dict2)
OUTPUT:
{'A': 'Apple', 'B': 'Banana', 'G': 'Grapes', 'M': 'Mango'}
{'G': 'Grapes', 'M': 'Mango'}
8. clear()
i)
d={100:1, 200:2, 300:3}
d.clear()
print(d)
OUTPUT:
{}
OUTPUT:
None
9. del
dic={'C':'Chocolate','I':'Icecream','B':'Biscuit','W':'Wafer'}
del dic['W']
print(dic)
OUTPUT:
{'C': 'Chocolate', 'I': 'Icecream', 'B': 'Biscuit'}
10. pop()
dic={'T':'Tea','C':'Coffee','J':'Juice'}
print(dic.pop('T'))
OUTPUT:
Tea