Tuples and Dictionaries
Tuples and Dictionaries
1
C2TI Tuples and Dictionaries
❖ Membership:
▪ The in operator checks if the element is present in the tuple and returns
True, else it returns False.
▪ The not in operator returns True if the element is not present in the tuple,
else it returns False.
❖ Slicing:
▪ Like string and list, slicing can be applied to tuples also.
❖ Tuple Methods and Built-in Functions:
▪ Python provides many functions to work on tuples.
Method Description Example
➢ Tuple Assignment:
• Assignment of tuple is a useful feature in Python.
2
C2TI Tuples and Dictionaries
3
C2TI Tuples and Dictionaries
➢ Dictionary Operations:
❖ Membership:
▪ The membership operator in checks if the key is present in the dictionary
and returns True, else it returns False.
▪ The not in operator returns True if the key is not present in the dictionary,
else it returns False.
➢ Traversing a Dictionary:
• We can access each item of the dictionary or traverse a dictionary using for
loop.
Method 1:
print(key,':',dict1[key])
Mohan: 95
Ram: 89
Suhel: 92
Sangeeta: 85
Method 2:
print(key,':',value)
Mohan: 95
Ram: 89
Suhel: 92
Sangeeta: 85
4
C2TI Tuples and Dictionaries
len() Returns the length or number of key: >>> dict1 = {'Mohan':95,'Ram':89, 'Suhel':92,
value pairs of the dictionary passed 'Sangeeta':85}
as the argument >>> len(dict1)
4
dict() Creates a dictionary from a pair1 = [('Mohan',95),('Ram',89),
sequence of key-value pairs ('Suhel',92),('Sangeeta',85)]
>>> pair1
[('Mohan', 95), ('Ram', 89), ('Suhel', 92),
('Sangeeta', 85)]
keys() Returns a list of keys in the >>> dict1 = {'Mohan':95, 'Ram':89, 'Suhel':92,
dictionary 'Sangeeta':85}
>>> dict1.keys()
dict_keys(['Mohan', 'Ram', 'Suhel', 'Sangeeta'])
values() Returns a list of values in the >>> dict1 = {'Mohan':95, 'Ram':89, 'Suhel':92,
dictionary 'Sangeeta':85}
>>> dict1.values()
dict_values([95, 89, 92, 85])
items() Returns a list of tuples(key – value) >>> dict1 = {'Mohan':95, 'Ram':89, 'Suhel':92,
pair 'Sangeeta':85}
>>> dict1.items()
dict_items([( 'Mohan', 95), ('Ram', 89), ('Suhel',
92), ('Sangeeta', 85)])
get() Returns the value corresponding to >>> dict1 = {'Mohan':95, 'Ram':89, 'Suhel':92,
the key passed as the argument If 'Sangeeta':85}
the key is not present in the >>> dict1.get('Sangeeta')
dictionary it will return None 85
update() appends the key-value pair of the >>> dict1 = {'Mohan':95, 'Ram':89, 'Suhel':92,
dictionary passed as the argument 'Sangeeta':85}
to the key-value pair of the given >>> dict2 = {'Sohan':79,'Geeta':89}
dictionary >>> dict1.update(dict2)
>>> dict1
{'Mohan': 95, 'Ram': 89, 'Suhel': 92, 'Sangeeta':
85, 'Sohan': 79, 'Geeta': 89}
>>> dict2
{'Sohan': 79, 'Geeta': 89}
del() Deletes the item with the given key >>> dict1 = {'Mohan':95,'Ram':89, 'Suhel':92,
To delete the dictionary from the 'Sangeeta':85}
memory we write: >>> del dict1['Ram']
>>> dict1 {'Mohan':95,'Suhel':92, 'Sangeeta': 85}
del Dict_name
clear() Deletes or clear all the items of the >>> dict1 = {'Mohan':95,'Ram':89, 'Suhel':92,
dictionary 'Sangeeta':85}
>>> dict1.clear()
>>> dict1
{}
5
C2TI Tuples and Dictionaries
➢ Manipulating Dictionaries:
• We have learnt how to create a dictionary and apply various methods to
manipulate it (refer the table previously given).