Dictionaries in Python

A dictionary is collection of values indexed by keys

A dictionary is collection of values indexed by keys.
Keys cannot be repeated, values can be repeated
Empty Dictionaries are created by {} brackets or dict()
"""
d = {}
print(d, type(d))
d = dict()
print(d, type(d))
"""
Dictionaries with keys
d={1:"One",2:"Two"}
1 is key and "One" is value
"""
d = {1: "One", 2: "Two"}
print(d)
# Add a key value to dictionary using d[key]=value. Updating is done in the same way
d[3] = "Three"
print(d)
d[3] = "3-Three"  # Replace
print(d)
"""
dictionary has three collections Items, Keys and Values
"""
print("Items")
for key, value in d.items():
    print(key, value)
print("Keys")
for key in d.keys():
    print(key)
print("Values")
for value in d.values():
    print(value)
# Searching for values. get method returns None if value is not found
value = d.get(0)
print(value)  # None because key 0 does not exist
value = d.get(1)
print(value)  # Key exists
# We cn also search by direct indexing but it throws exception if not found
print(d[2])  # Value found
# print(d[9])#raises KeyError because not found
"""
Removing elements
"""
d.pop(2)  # Throws exception if key not found
print(d)
d.popitem()  # Remove the last element
print(d)


runs = [1, 10, 23, 34, 56]
# Create a dictionary from 2 lists
names = ["Sachin", "Rahul", "Mahender", "Sourav", "Saubhagya"]
newdict1 = {name: run for (name, run) in zip(names, runs)}
print(newdict1)
newdict2 = {run: name for (run, name) in zip(runs, names)}
print(newdict2)
newdict3 = {run: name for (run, name) in zip(runs, [run * 2 for run in runs])}
print(newdict3)
newdict4 = {run: name for (run, name) in zip(runs, [(lambda run: run + 2)(run) for run in runs])}
print(newdict4)
newdict5 = dict.fromkeys(runs)
# Create dictionary from a list with values = None
print(newdict5)
newdict6 = dict.fromkeys(runs,0)
# Create dictionary from a list with values = 0
print(newdict6)




Questions

  • Write a program to remove all duplicates from a sentence using a Dictionary
    This train is a good train
    This train is a good
  • Implement the Counter function that creates a key:count dictionary
    ba ba black sheep
    {"ba":2,"black":1,"sheep":1}
  • Convert key:value to value:key dictionary
    {0:"zero",1:"one"}
    to {"zero":0,"one":1}
  • Write a program to create a dictionary of factorials
    {0:1,1:1,2:2,3:6,4;24}
  • Write a program to store Student records in a dictionary using rollno as key.Use a nested dictionary as data.
    {1:{"name":"Mahender","class":10},2:{"name":"Dharmendra","class":10}}
    Provide add,update, delete and search. Rollno is unique
  • Write a program to store a list of powers of a number in a dictionary
    For 2={0:1,1:2,2:4,3:8}
  • Write a program to merge 2 dictionaries.
    {1;:"One",2:"Two"} + 
    {3:"Three"}
    {1:"One",2:"Two",3:"Three"}




Contact us for software training, education or development










 

Post a Comment

0 Comments