Skip to main content

Command Palette

Search for a command to run...

Lecture11 # Dictionary Data Type in Python

Updated
2 min read
Lecture11 # Dictionary Data Type in Python
H

My name is Hamza Rehman. I'm a passionate DevOps enthusiast. With a deep interest in open-source technologies and automation, I enjoys to share my knowledge and insights with the community.

In Python, a dictionary is a built-in data type that allows you to store collections of key-value pairs. Each key in a dictionary is unique, and it maps to a corresponding value. Dictionaries are incredibly versatile and are often used to represent data structures like maps, tables, or simple databases.

My Python codes: https://masterwithhamza@bitbucket.org/hamxaflutterapps/mypythoncodes.git

Creating a Dictionary

my_dict = {'name': 'Alice', 'age': 30, 'city': 'New York'}

Accessing Dictionary Elements

You can access values in a dictionary using their keys:

name = my_dict['name']  # Access the value associated with the key 'name' ('Alice')
age = my_dict['age']    # Access the value associated with the key 'age' (30)

Modifying and Adding to a Dictionary

Dictionaries are mutable, so you can change their contents:

my_dict['age'] = 31  # Modify the 'age' value to 31
my_dict['gender'] = 'Female'  # Add a new key-value pair

Removing Items from a Dictionary

You can remove a key-value pair from a dictionary using the del statement or the pop() method:

del my_dict['city']  # Remove the 'city' key-value pair
my_dict.pop('age')   # Remove the 'age' key-value pair and return its value

Conclusion

In Python, dictionaries are powerful data structures for organizing and managing collections of key-value pairs. They provide flexibility, efficiency, and easy access to data. Dictionaries are commonly used for tasks like configuration settings, data processing, and representing complex relationships between entities. By understanding how to create, access, modify, and work with dictionaries, you can handle a wide range of data-related tasks in Python.

More from this blog

M

MasterWithHamza

115 posts

My name is Hamza Rehman. I'm a passionate DevOps enthusiast. With a deep interest in open-source technologies and automation,i enjoys sharing my knowledge and insights with the community.