-
Notifications
You must be signed in to change notification settings - Fork 4
/
DictionaryExample.py
32 lines (27 loc) · 964 Bytes
/
DictionaryExample.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# Dictionary is collection of key value pairs of items like Hashmap and hashtable in java likewise
# Keys cannot be duplicated, if duplcated then it will refer latest added entry
di = dict({1:"one", 2:"two", 3:"three",4:"four"});
print(di);
#Another way of delcaring dictionary
d = {1:"one", 2:"two", 3:"three",4:"four"}
print(d)
capitals = {"Karnataka":"Bangalore","Telengana":"Hyderabad"}
print(capitals)
capitals["Maharastra"] = "Mumbai";
print(capitals)
capitals["NewDehli"] = "Dehli"
capitals["TamilNadu"] = "Chennai"
capitals["TamilNadu"] = "Chennai2"
capitals["TamilNadu2"] = "Chennai"
for item in capitals:
print(item, " is state and capital is ", capitals[item])
print(capitals.keys())
print(capitals.values())
print(list(capitals.keys()))
print(list(capitals.values()))
print(list(capitals))
print(capitals.get("Karnataka"))
print(capitals.get("Kerala", "Capital not defined"))
# Dictionary Comprehension
d = {j:None for j in range(10)}
print(d)