Unit Iii List, Tuples, Set and Dictionaries
Unit Iii List, Tuples, Set and Dictionaries
Unit Iii List, Tuples, Set and Dictionaries
Python List: Introduction, accessing List, List operations, Working with Lists, List functions and
methods Python Tuple:- Introduction, accessing Tuple, operations on Tuple, Working with Tuple
,Functions and Methods, Python Set - Introduction, accessing Set, Set operations, working with
Set, Functions and Methods, Python Dictionaries – Introduction, working with dictionaries,
Properties, Functions. Dictionaries Operations, List Comprehension.
Python Dictionaries: Dictionary is a Data Structure in which we can store values as a pair of
key and value. Each key is separated from its value by a colon(:), and consecutive items are
separated by commas. The entire items in a dictionary are enclosed in curly brackets { }. The
syntax for defining a dictionary is:
Dictionary_name = {kaey1:value1,key2:value2,……}
keys in the dictionary must be unique and be of any immutable data type, there is no stringent
requirement for uniqueness and type of values. That is value of a key can be of any type.
Dictionaries are not sequence, rather they are mapping. Mappings are collections of objects that
store objects by key instead of by relative position.
dic = {}
print("Dictonary = ",dic)
Empty Dictonary = {}
Dictionary Dic = {1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16, 9: 18, 10: 20}
Access Values: To access values in a Dictionary, square brackets are used along with the key to
obtain the value.
To add a new entry or a key value pair in a dictionary just specify key value pair as an existing
key value pair. To modify an entry, just overwrite the existing value.
print("Dictionary1 = ",dic1)
print("Dictionary2 = ",dic2)
print("Looping in Dictionary1")
Output:
After Updation Dictionary1 = {'HTNO': '2103A5', 'Name': 'Ravi', 'Course': 'B.Tech C.S.E.',
'Year': 'Ist year', 'Semester': 'IInd Semester'}
After Updation Dictionary2 = {'HTNO': '2103A4', 'Name': 'Kumar', 'Course': 'B.Tech E.C.E.',
'Year': 'Ist year', 'Semester': 'IInd Semester'}
After Modification Dictionary2 = {'HTNO': '2103A4', 'Name': 'Kumar', 'Course': 'B.Tech
C.S.E.', 'Year': 'Ist year', 'Semester': 'IInd Semester'}
Looping in Dictionary1
Dictionary1 : HTNO 2103A5 Name Ravi Course B.Tech C.S.E. Year Ist year
Semester IInd Semester
Dictionary2 : HTNO 2103A4 Name Kumar Course B.Tech C.S.E. Year Ist year
Semester IInd Semester
Deleting Items: we can delete one or more items using the del keyword. To delete or remove all
items using clear() function. Finally we can remove an entire Dictonary from memory again
using del with Dictionary name. the pop() method removes an item from the dictionary and
returns its value, if the key is present. Otherwise KeyError Occurs.
The keys() Method of Dictionary returns a list of all the keys used in the dictionary in an
arbitrary order. The sorted() function is used to sort the keys in sorted order.
print("Dictionary1 = ",dic1)
del(dic1['Course'])
dic1.pop('Name')
print("Dictionay2 = ",dic2)
dic2.clear()
print("After Clear() Function Diction2 = ",dic2)
del dic2
Output:
subjects = ['CSA','C++','DS','OS']
marks = dict.fromkeys(subjects,-1)
print(marks)
print("2.Dict.get(key) Method()")
print("Dictionary1 = ",dic1)
Output:
1. Dict.fromkeys() function
2.Dict.get(key) Method()
Dictionary1 = {'HTNO': '2103A5', 'Name': 'Ravi', 'Course': 'B.Tech C.S.E.', 'Year': ''}
3. Dict.has_key(key) Method
4. Dict.Items() Method