Python
Python
Functions/Methods in Python
Introduction to Dictionary Methods
• Example:
• my_dict = {'A': 1, 'B': 2, 'C': 3}
• my_dict.clear()
• print(my_dict)
• Output:
• {}
2. get()
• Example:
• d = {'Name': 'Ram', 'Age': 19, 'Country': 'India'}
• print(d.get('Name'))
• print(d.get('Gender'))
• Output:
• Ram
• None
3. items()
• Example:
• d = {'Name': 'Ram', 'Age': 19, 'Country': 'India'}
• print(list(d.items()))
• Output:
• [('Name', 'Ram'), ('Age', 19), ('Country', 'India')]
4. keys()
• Example:
• d = {'Name': 'Ram', 'Age': 19, 'Country': 'India'}
• print(list(d.keys()))
• Output:
• ['Name', 'Age', 'Country']
5. update()
• Example:
• d1 = {'Name': 'Ram', 'Age': 19}
• d2 = {'Country': 'India'}
• d1.update(d2)
• print(d1)
• Output:
• {'Name': 'Ram', 'Age': 19, 'Country': 'India'}
6. values()
• Example:
• d = {'Name': 'Ram', 'Age': 19, 'Country': 'India'}
• print(list(d.values()))
• Output:
• ['Ram', 19, 'India']
7. pop()
• Example:
• d = {'Name': 'Ram', 'Age': 19, 'Country': 'India'}
• d.pop('Age')
• print(d)
• Output:
• {'Name': 'Ram', 'Country': 'India'}
8. popitem()
• Example:
• d = {'Name': 'Ram', 'Age': 19, 'Country': 'India'}
• val = d.popitem()
• print(val)
• Output:
• ('Country', 'India')