Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
Python dictionaries have several built-in methods that allow you to perform various operations on them. Here is a list of commonly used dictionary methods:
clear()
: Removes all items from the dictionary.
my_dict = {'a': 1, 'b': 2, 'c': 3} my_dict.clear() print(my_dict) # Output: {}
copy()
: Returns a shallow copy of the dictionary.
original = {'a': 1, 'b': 2, 'c': 3} copied = original.copy() print(copied) # Output: {'a': 1, 'b': 2, 'c': 3}
get(key, default=None)
: Returns the value for the specified key if it exists, otherwise returns the default value.
my_dict = {'a': 1, 'b': 2, 'c': 3} value = my_dict.get('d', 'default') print(value) # Output: 'default'
items()
: Returns a view object displaying a list of the dictionary's key-value pairs as tuples.
my_dict = {'a': 1, 'b': 2, 'c': 3} for key, value in my_dict.items(): print(key, value)
keys()
: Returns a view object displaying a list of the dictionary's keys.
my_dict = {'a': 1, 'b': 2, 'c': 3} for key in my_dict.keys(): print(key)
values()
: Returns a view object displaying a list of the dictionary's values.
my_dict = {'a': 1, 'b': 2, 'c': 3} for value in my_dict.values(): print(value)
pop(key, default=None)
: Removes and returns the value for the specified key. If the key is not found and a default value is provided, it returns the default value; otherwise, it raises a KeyError.
my_dict = {'a': 1, 'b': 2, 'c': 3} value = my_dict.pop('b') print(value) # Output: 2 print(my_dict) # Output: {'a': 1, 'c': 3}
popitem()
: Removes and returns an arbitrary key-value pair as a tuple. If the dictionary is empty, it raises a KeyError.
my_dict = {'a': 1, 'b': 2, 'c': 3} key, value = my_dict.popitem() print(key, value)
setdefault(key, default=None)
: Returns the value for the specified key if it exists, otherwise sets the key to the specified default value and returns the default value.
my_dict = {'a': 1, 'b': 2, 'c': 3} value = my_dict.setdefault('d', 4) print(value) # Output: 4 print(my_dict) # Output: {'a': 1, 'b': 2, 'c': 3, 'd': 4}
Common Dictionary Operations in Python:
# Example my_dict = {'a': 1, 'b': 2, 'c': 3}
Python dict keys()
Method:
keys()
method returns a view of all keys in the dictionary.# Example keys = my_dict.keys()
values()
and items()
Methods in Python Dictionaries:
values()
method returns a view of all values, and items()
returns key-value pairs.# Example values = my_dict.values() items = my_dict.items()
How to Use get()
Method with Dictionaries in Python:
get()
method retrieves the value for a given key, or a default value if the key is not present.# Example value = my_dict.get('a', 'Key not found')
Updating and Modifying Dictionaries in Python:
# Example my_dict['d'] = 4 # Add a new key-value pair my_dict['a'] = 10 # Update an existing value
Python Dictionary pop()
and popitem()
Methods:
pop()
method removes and returns the value for a given key.popitem()
method removes and returns the last key-value pair.# Example popped_value = my_dict.pop('b') popped_item = my_dict.popitem()
Dictionary Comprehension vs dict
Methods in Python:
keys()
, values()
, items()
) provide dynamic views on dictionary contents.# Example squared_dict_comp = {key: value**2 for key, value in my_dict.items()} squared_dict_method = dict((key, value**2) for key, value in my_dict.items())