In Python, Dictionary is a built-in data structure that stores a collection of key-value pairs.
Dictionaries are unordered, mutable, and can contain elements of different data types.
Each element in a dictionary is accessed by its key rather than by its index.
Here's how to work with dictionaries in Python:
We can create a dictionary by enclosing comma-separated key-value pairs within curly braces {}.
# Syntax dictVar = {} # Checking Type of Variable with Empty dictionary object print(type(dictVar)) # Output: <class 'dict'> your_dict = {'name': 'Alice', 'age': 24, 'city': 'London'} # Printing dictionary variable print(your_dict) # Output: {'name': 'Alice', 'age': 24, 'city': 'London'} # Checking Type of Variable print(type(your_dict)) # Output: <class 'dict'>
We can access elements of a dictionary by specifying the key within square brackets [].
your_dict = {'name': 'Alice', 'age': 24, 'city': 'London'} print(your_dict['name']) # Output: 'Alice' print(your_dict['age']) # Output: 24 print(your_dict['city']) # Output: 'London'
We can modify the value associated with a key in a dictionary by assigning a new value to it.
your_dict = {'name': 'Alice', 'age': 24, 'city': 'London'} your_dict['age'] = 42 your_dict['city'] = 'New York' print(your_dict) # Output: {'name': 'Alice', 'age': 42, 'city': 'New York'}
We can add new key-value pairs to a dictionary by assigning a value to a new key.
your_dict = {'name': 'Alice', 'age': 24, 'city': 'London'} your_dict['gender'] = 'Female' print(your_dict) # Output: {'name': 'Alice', 'age': 24, 'city': 'London', 'gender': 'Female'}
We can remove a key-value pair from a dictionary using the "del" keyword or the "pop()" method.
your_dict = {'name': 'Alice', 'age': 24, 'city': 'London', 'gender': 'Female'} # Removing attribute using del keyword del your_dict['city'] print(your_dict) # Output: {'name': 'Alice', 'age': 24, 'gender': 'Female'} # Removing attribute using pop method removed_value = your_dict.pop('age') print(removed_value) # Output: 24
Dictionaries support various methods for common operations such as accessing keys, values, items and performing membership testing.
Methods are keys(), values() and items() to retreive keys and values and both from dictionary.
your_dict = {'name': 'Alice', 'age': 24, 'gender': 'Female'} # Accessing keys, values, and items print(your_dict.keys()) # Output: dict_keys(['name', 'age', 'gender']) print(your_dict.values()) # Output: dict_values(['Alice', 24, 'Female']) print(your_dict.items()) # Output: dict_items([('name', 'Alice'), ('age', 24), ('gender', 'Female')])
key name followed by `in` keyword and tuple collection, we can check key exists in our tuple collection.
your_dict = {'name': 'Alice', 'age': 24, 'gender': 'Female'} # Checking Attributes Exists in Tuple print('name' in your_dict) # Output: True print('city' in your_dict) # Output: False
Dictionaries are widely used in Python for tasks such as storing key-value pairs, representing mappings, and organizing data in a structured manner.
They are efficient for lookups and are an essential tool in Python programming.