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)
In Python, dictionaries (or "dicts") are mutable, unordered collections of key-value pairs. They are also known as associative arrays, hash maps, or hash tables in other programming languages. Dictionaries are efficient for looking up values based on keys.
Creating dictionaries:
You can create a dictionary using curly braces ({}
) and separating keys and values with colons. Separate multiple key-value pairs with commas.
Example:
my_dict = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
You can also create an empty dictionary using the dict()
constructor:
empty_dict = dict()
Accessing and modifying values:
To access the value associated with a key, use the index operator []
:
my_dict = {'key1': 'value1', 'key2': 'value2'} print(my_dict['key1']) # Output: 'value1'
To modify a value, assign a new value to the key:
my_dict = {'key1': 'value1', 'key2': 'value2'} my_dict['key1'] = 'new_value1' print(my_dict) # Output: {'key1': 'new_value1', 'key2': 'value2'}
Adding and removing key-value pairs:
To add a new key-value pair, simply assign a value to a new key:
my_dict = {'key1': 'value1', 'key2': 'value2'} my_dict['key3'] = 'value3' print(my_dict) # Output: {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
To remove a key-value pair, use the del
keyword:
my_dict = {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'} del my_dict['key1'] print(my_dict) # Output: {'key2': 'value2', 'key3': 'value3'}
Checking for keys:
To check if a key exists in the dictionary, use the in
keyword:
my_dict = {'key1': 'value1', 'key2': 'value2'} if 'key1' in my_dict: print("Key1 is in the dictionary") else: print("Key1 is not in the dictionary")
Iterating over dictionaries:
You can iterate over keys, values, or both (key-value pairs) using various dictionary methods:
Iterate over keys:
my_dict = {'key1': 'value1', 'key2': 'value2'} for key in my_dict.keys(): print(key)
Iterate over values:
my_dict = {'key1': 'value1', 'key2': 'value2'} for value in my_dict.values(): print(value)
Iterate over key-value pairs:
my_dict = {'key1': 'value1', 'key2': 'value2'} for key, value in my_dict.items(): print(key, value)
Dictionary methods:
Some useful dictionary methods include:
keys()
: Returns a view object displaying a list of the dictionary's keys.values()
: Returns a view object displaying a list of the dictionary's values.items()
: Returns a view object displaying a list of the dictionary's key-value pairs as tuples.How to Create a Dictionary in Python:
{}
to create a dictionary with key-value pairs.# Example my_dict = {'name': 'John', 'age': 25, 'city': 'New York'}
Dictionary Data Type in Python:
# Example student_info = {'name': 'Alice', 'grade': 'A', 'age': 20}
Python dict
Methods and Operations:
keys()
, values()
, and items()
to access dictionary information.# Example keys = student_info.keys()
Accessing and Modifying Dictionary Elements in Python:
# Example print(student_info['name']) # Access student_info['age'] = 21 # Modify student_info['grade'] = 'B' # Add
Nested Dictionaries in Python:
# Example nested_dict = {'person': {'name': 'Bob', 'age': 30}}
Iterating Through a Dictionary in Python:
# Example for key in student_info: print(key, student_info[key])
Dictionary Comprehension in Python:
# Example squares = {x: x**2 for x in range(5)}
Merging Dictionaries in Python:
update()
method or unpacking (**
) to merge dictionaries.# Example dict1 = {'a': 1, 'b': 2} dict2 = {'b': 3, 'c': 4} dict1.update(dict2) # Merging using update
Deleting Elements from a Dictionary in Python:
del
to delete a specific key-value pair or clear()
to remove all items.# Example del student_info['grade'] # Deleting a specific key-value pair