UNIT-III - Part-3 (Dict and Sets)
UNIT-III - Part-3 (Dict and Sets)
Solving
Unit-3
Ex:
my_dict = {1:‘Hello', 2:‘Hi',
3:{'A':'Welcome', 'B':'To', 'C':‘Vignan'}}
print(my_dict)
Department of Electrical and Electronics Engineering
Nested Dictionary
A nested dictionary can also be created as.
Ex:
myfamily = {
"child1" : {
"name" : "Emil",
"year" : 2004
},
"child2" : {
"name" : "Tobias",
"year" : 2007
},
"child3" : {
"name" : "Linus",
"year" : 2011
}
}
Department of Electrical and Electronics Engineering
Nested Dictionary
A nested dictionary can also be created as.
Ex:
child1 = {
"name" : "Emil",
"year" : 2004 myfamily = {
} "child1" : child1,
child2 = { "child2" : child2,
"name" : "Tobias", "child3" : child3
"year" : 2007 }
}
child3 = {
"name" : "Linus",
"year" : 2011
}
• In order to access the value of any key in nested dictionary, use indexing
[] syntax.
Ex:
my_dict = {'D1':{1:‘Hello'},'D2':{'Name':‘Vignan'}}
print(my_dict['D1'])
print(my_dict['D1'][1])
print(my_dict['D2']['Name'])
Function Description
Return True if all keys of the dictionary are True (or if the
all()
dictionary is empty).
Ex:
squares = {0: 0, 1: 1, 3: 9, 5: 25, 7: 49, 9: 81}
print(all(squares))
print(any(squares))
print(len(squares))
print(sorted(squares))
Output:
False
True
6
[0, 1, 3, 5, 7, 9]
•We can test if a key is in a dictionary or not using the keyword in.
•Membership test is for keys only, not for values.
Ex:
squares = {1: 1, 3: 9, 5: 25, 7: 49, 9: 81}
print(1 in squares) # Output: True
print(2 in squares) # Output: False
print(49 in squares) # Output: False
clear():
The clear() method removes all the elements from a dictionary.
Syntax: dictionary.clear()
Parameter Values: No parameters
Ex:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
car.clear()
print(car)
copy():
The copy() method returns a copy of the specified dictionary.
Syntax: dictionary.copy()
Parameter Values: No parameters
Ex:
car = {
"brand": "Ford",
"model": "Mustang",
"year": 1964
}
x=car.copy()
print(x)
d={}
for i in range(3):
k=input("enter key")
v=input("enter value")
d1={k:v}
d.update(d1)
print(d)
dict1={1:10, 2:20}
dict2={3:30, 4:40}
dict3={5:50, 6:60}
dict4 = {}
for d in (dict1, dict2, dict3):
dict4.update(d)
print(dict4)
Ex:
thisset = {"apple", "banana", "cherry"}
for x in thisset:
print(x)
Method Description
difference_update() Removes all elements of another set from this set
intersection_update(
Updates the set with the intersection of itself and another
)
symmetric_differenc Updates a set with the symmetric difference of itself and
e_update() another
isdisjoint() Returns True if two sets have a null intersection
issubset() Returns True if another set contains this set
issuperset() Returns True if this set contains another set
Output: True
True
Output: True
False
Output: False
True
Ex2:
ste_strns = {‘a’, ‘b’, ‘c’, ‘d’}
print(len(ste_strns)) print(all(st_num))
print(max(ste_strns)) print(any(st_num))
print(min(ste_strns)) print(sorted(st_num))
#not possible
•We can test if an item is in a set or not using the keyword in.
Ex:
my_set = set("apple")
print('a' in my_set) # Output: True
print('p' not in my_set) # Output: False