Python Dictionary Tutorial, Dictionaries are one of the most powerful and versatile data structures in Python. They allow you to store and manage data in key-value pairs, making it easy to retrieve and manipulate information efficiently. Whether you’re a beginner or an experienced developer, understanding dictionaries is essential for writing clean and effective Python code. In this tutorial, we’ll cover everything you need to know about Python dictionaries, from basic operations to advanced techniques.
What are Python Dictionary?
At its core, a Python dictionary is a collection of key-value pairs. Think of it like a real-world dictionary: you look up a word (the “key”) to find its definition (the “value”). In Python, keys must be unique and immutable (like strings, numbers, or tuples), while values can be of any data type.
Creating a Python Dictionary
You can create a dictionary using curly braces {} or the dict() constructor.
# Using curly braces cr_dict = {"name": "Raunak Gupta", "age": 45, "city": "Kolkata"} # Using the dict() constructor cro_dict = dict(name="Raunak Gupta", age=27, city="New Delhi")
Accessing Values in a Python dictionary
To access a value in a dictionary, you use the corresponding key within square brackets [].
# Creating a simple dictionary person = { "name": "Sanjay", "age": 30, "city": "Buxar" } print(person["name"]) # Output: Sanjay print(person["age"]) # Output: 30 #Using .get() to avoid errors if the key doesn't exist. print(person.get("job", "Unknown")) #Output: Unknown, since job doesn't exist.
Adding or Modifying a Python Dictionary
You can add, update, or delete key-value pairs easily.
my_dict = {"name": "Virat", "age": 14, "city": "Kanchrapara"} # Adding a new key-value pair my_dict["email"] = "[email protected]" # Updating an existing key my_dict["age"] = 26 # Removing a key-value pair my_dict.pop("city") # Using del to remove a key del my_dict["email"]
Iterating Through a Python Dictionary
You can loop through a dictionary using for loops to access keys, values, or both.
student = {"name": "SANJAY", "age": 21, "course": "Computer Science","grade": "A"} # Iterating through keys for key in student: print(key) # Output: name, age, course # Iterating through values for value in student.values(): print(value) # Output: SANJAY, 21, Computer Science # Iterating through key-value pairs for key, value in student.items(): print(f"{key}: {value}") # Output: name: SANJAY, age: 21, course: Computer Science
Python Dictionary Methods
Python dictionaries come with a variety of useful methods:
- keys(): Returns a view object containing the dictionary’s keys.
- values(): Returns a view object containing the dictionary’s values.
- items(): Returns a view object containing 1 key-value pairs as tuples.
COMPUTER SCIENCE WITH PYTHON BOOKS DOWNLOAD
If you have and query or suggestion or need assistance then please contact me, I will reply to fix your problem, if you like our content then you can subscribe to our Youtube channel. If you want to hire me then reach us at our Fiverr.