0% found this document useful (0 votes)
4 views

Python

The document provides an overview of built-in dictionary methods in Python, which allow users to manage key-value pairs effectively. Key methods include clear(), get(), items(), keys(), update(), values(), pop(), and popitem(), each with examples demonstrating their functionality. These methods facilitate data modification, retrieval, and management within dictionaries.

Uploaded by

hangesantosh39
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Python

The document provides an overview of built-in dictionary methods in Python, which allow users to manage key-value pairs effectively. Key methods include clear(), get(), items(), keys(), update(), values(), pop(), and popitem(), each with examples demonstrating their functionality. These methods facilitate data modification, retrieval, and management within dictionaries.

Uploaded by

hangesantosh39
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Built-in Dictionary

Functions/Methods in Python
Introduction to Dictionary Methods

• Python dictionaries store data as key-value pairs.


• They provide built-in methods to modify, access, and manage data
efficiently.
• These methods help in removing, updating, and retrieving data
from a dictionary.
• Let's explore some of the key built-in dictionary methods.
List of Built-in Dictionary Methods

• 1. clear() - Removes all items from the dictionary


• 2. copy() - Returns a shallow copy of the dictionary
• 3. fromkeys() - Creates a dictionary from given keys
• 4. get() - Returns the value of a specified key
• 5. items() - Returns key-value pairs as tuples
• 6. keys() - Returns all keys in the dictionary
• 7. pop() - Removes and returns an element with a given key
• 8. popitem() - Removes and returns the last inserted item
• 9. setdefault() - Returns the value of a key or sets a default
• 10. update() - Updates the dictionary with another dictionary
1. clear()

• Removes all items from the dictionary.

• Example:
• my_dict = {'A': 1, 'B': 2, 'C': 3}
• my_dict.clear()
• print(my_dict)

• Output:
• {}
2. get()

• Returns the value for the given key.

• Example:
• d = {'Name': 'Ram', 'Age': 19, 'Country': 'India'}
• print(d.get('Name'))
• print(d.get('Gender'))

• Output:
• Ram
• None
3. items()

• Returns all dictionary key-value pairs as a list of tuples.

• Example:
• d = {'Name': 'Ram', 'Age': 19, 'Country': 'India'}
• print(list(d.items()))

• Output:
• [('Name', 'Ram'), ('Age', 19), ('Country', 'India')]
4. keys()

• Returns a list of all dictionary keys.

• Example:
• d = {'Name': 'Ram', 'Age': 19, 'Country': 'India'}
• print(list(d.keys()))

• Output:
• ['Name', 'Age', 'Country']
5. update()

• Updates the dictionary with another dictionary's key-value pairs.

• Example:
• d1 = {'Name': 'Ram', 'Age': 19}
• d2 = {'Country': 'India'}
• d1.update(d2)
• print(d1)

• Output:
• {'Name': 'Ram', 'Age': 19, 'Country': 'India'}
6. values()

• Returns a list of all dictionary values.

• Example:
• d = {'Name': 'Ram', 'Age': 19, 'Country': 'India'}
• print(list(d.values()))

• Output:
• ['Ram', 19, 'India']
7. pop()

• Removes and returns the value for the given key.

• Example:
• d = {'Name': 'Ram', 'Age': 19, 'Country': 'India'}
• d.pop('Age')
• print(d)

• Output:
• {'Name': 'Ram', 'Country': 'India'}
8. popitem()

• Removes and returns the last inserted key-value pair.

• Example:
• d = {'Name': 'Ram', 'Age': 19, 'Country': 'India'}
• val = d.popitem()
• print(val)

• Output:
• ('Country', 'India')

You might also like