Introduction to Python

Python Dictionaries

Devesh Kayal , Head Content Creater - Python, DIG-IT.WORLD

Dictionaries are an important data structure in Python that allow you to store and retrieve data using key-value pairs. They are also known as associative arrays or hash maps in other programming languages. We will explore the basics of dictionaries, their creation, and some common operations you can perform on them.

Python Dictionaries

Dictionaries are an important data structure in Python that allow you to store and retrieve data using key-value pairs. They are also known as associative arrays or hash maps in other programming languages. We will explore the basics of dictionaries, their creation, and some common operations you can perform on them.

 

Creating a Dictionary:

You can create a dictionary in Python by enclosing comma-separated key-value pairs in curly braces {}. The key and value are separated by a colon :. Example:

In the example above, we created a dictionary called student with three key-value pairs: "name", "age", and "grade". The keys are "name", "age", and "grade", while the corresponding values are "John", 20, and "A", respectively.

 

Accessing Dictionary Values:

To access the value associated with a particular key in a dictionary, you can use the square bracket notation [] and provide the key. Example:

In the example above, we accessed the values associated with the keys "name", "age", and "grade" in the student dictionary.

 

Modifying Dictionary Values:

Dictionaries are mutable, which means you can modify their values after they are created. To modify the value associated with a key, you can use the assignment operator =. Example:

In the example above, we modified the value associated with the key "grade" in the student dictionary from "A" to "B".

 

Adding New Key-Value Pairs:

You can add new key-value pairs to a dictionary by assigning a value to a new key. If the key already exists, the value will be updated. Example:

In the example above, we added two new key-value pairs to the student dictionary: "city" and "major".

 

Dictionary Methods:

Python provides several methods that you can use to perform various operations on dictionaries. Here are a few commonly used methods:

  • keys(): Returns a list of all the keys in the dictionary.
  • values(): Returns a list of all the values in the dictionary.
  • items(): Returns a list of key-value pairs as tuples.

This is a code that demonstrates these methods:

In the example above, we used the keys(), values(), and items() methods on the student dictionary to retrieve the keys, values, and key-value pairs, respectively.

 

Dictionaries are versatile data structures in Python that allow you to store and retrieve data using key-value pairs. They are useful when you need to associate information with specific keys. Remember, dictionaries are unordered collections, so the order of key-value pairs may not be preserved. Additionally, keys in a dictionary must be unique.