Dictionary is a versatile in-built data type in python. Dictionary are mutable, unordered collection with elements in the form of a key:value pairs that associated key to values.
Structure:
dictionary-name={key:value, key:value...}
Example:
student={"name":"John","roll":12,"Percentage":88.25}
Characters of a Dictionary:
- Dictionary are undordered set of key:value pairs
- Dictionary are not sequences like list,tuple,string etc as they are not indexed by number
- Keys of the dictionary MUST BE UNIQUE
- Dictionary are MUTABLE i.e. we can change the value of the key in place
- Internally the key and value are mapped(connected) to each other by using some hash function. This way of linking is called mapping.
stu={12:"John",13:"tom",14:"jason"} #here roll acts keys and name acts as values
print(stu)
Note: The keys of the dictionary must be of immutable type such as string,number,tuple
Creating dictionary using dict() function:
rl=[12,13,14]
nm=['John','Tom','Jason']
stu=dict(Roll=rl,Name=nm)
print(stu)
stu={12:"John",13:"tom",14:"jason"}
print(stu[14])
print(stu.keys())
print(stu.values())
Traversing using loop:
for key in stu:
print(key,' : ',stu[key])
for key,val in stu.items():
print(key,' : ',val)
for key in stu.keys():
print(key)
for key in stu.values():
print(key)
stu={12:"John",13:"tom",14:"jason"}
stu[15]="Harry"
print(stu)
stu={12:"John",13:"tom",14:"jason",15: 'Harry'}
stu[14]="Dwayn"
print(stu)
stu={12:"John",13:"tom",14:"jason"}
print(stu)
del stu[14]
print(stu)
stu={12:"John",13:"tom",14:"jason"}
print(stu)
val=stu.pop(14)
print(stu)
print(val)
len(dictionary_name): Return the number of key:value pair in the dictionary
stu={12:"John",13:"tom",14:"jason"}
print(len(stu))
dict(**kwargs): This function creates a dictionary
dc=dict(x=5, y=0)
print(dc)
dictionary_name.keys() Returns all the keys of the dictionary
stu={12:"John",13:"tom",14:"jason"}
print(stu.keys())
dictionary_name.values() Returns all the values of the dictionary
stu={12:"John",13:"tom",14:"jason"}
print(stu.values())
dictionary_name.items() Returns all the key:value pair in the dictionary
stu={12:"John",13:"tom",14:"jason"}
print(stu.items())
dictionary_name.get(key,default) Return the value of the key if present else return the Default value
stu={12:"John",13:"tom",14:"jason"}
print(stu.get(14,'Value not present'))
print(stu.get(15,'Value not present'))
dictionary1.update(dictionary2): Updates the content of dictionary1 from dictionary2 based on matching index
emp1={'name':'John','Salary':10000,'age':24}
emp2={'name':'Diya','Salary':25000,'dept':'sales'}
emp1.update(emp2)
print(emp1)
dictionary_name.clear() removes all the elements from the dictionary
stu={12:"John",13:"tom",14:"jason"}
stu.clear()
print(stu)
dict.fromkeys(keys, value) Method returns a dictionary with the specified keys and the specified value.
roll = (12,13,14)
name = ("John")
stu = dict.fromkeys(roll, name)
print(stu)
dictionary1.copy() Returns a shallow copy of the dictionary. It doesn't modify the original dictionary.
stu={12:"John",13:"tom",14:"jason"}
stu1=stu.copy()
print(stu1)
dictionary_name.popitem() Remove and return the last item of the dictionary
stu={12:"John",13:"tom",14:"jason"}
val=stu.popitem()
print(val)
max(dictionary_name) Return the maximum index from the dictionary
stu={12:"John",13:"tom",14:"jason"}
print(max(stu))
min(dictionary_name) Return the minimum index from the dictionary
stu={12:"John",13:"tom",14:"jason"}
print(min(stu))
- Write a program that repeatedly asks the user to enter product and price. Store all of these in a dictionary whose keys are the product names and whose values are the prices.
- Given two dictionaries say D1 and D2. Write a program that lists the overlapping keys of the dictionary, i.e. if a key of D1 is also a key of D2, then list it.
- write a program to count the number of times a character appears in a given string using a dictionary
- Write a program to create a dictionary with names of employees, their salary and access them.