Which Data Type Is Called Mapping in Python?

//

Scott Campbell

Python provides a powerful data type called mapping that allows you to store and manipulate collections of key-value pairs. In Python, this data type is referred to as a dictionary. A dictionary is an unordered collection of elements that are stored as key-value pairs, where each key is unique.

Creating a Dictionary

To create a dictionary in Python, you can use curly braces {} or the built-in dict() function. Let’s see some examples:

# Using curly braces
my_dict = {'name': 'John', 'age': 25, 'city': 'New York'}

# Using dict() function
my_dict = dict(name='John', age=25, city='New York')

In the above examples, we have created a dictionary named my_dict. It contains three key-value pairs: ‘name’: ‘John’, ‘age’: 25, and ‘city’: ‘New York’.

Accessing Values in a Dictionary

You can access the values in a dictionary by using the corresponding keys. Let’s see how:

# Accessing values by keys
print(my_dict['name'])  # Output: John
print(my_dict['age'])   # Output: 25
print(my_dict['city'])  # Output: New York

In the above code snippet, we are accessing the values of keys ‘name’, ‘age’, and ‘city’ using square brackets. This will print out the corresponding values from the dictionary.

Modifying Values in a Dictionary

You can modify the values in a dictionary by assigning a new value to the corresponding key. Let’s see an example:

# Modifying values
my_dict['age'] = 26

# Printing the modified value
print(my_dict['age'])  # Output: 26

In the above code snippet, we are modifying the value of the key ‘age’ from 25 to 26. The print statement will display the updated value.

Adding and Removing Key-Value Pairs

You can add new key-value pairs to a dictionary or remove existing ones using certain methods and operators. Let’s explore some examples:

# Adding a new key-value pair
my_dict['occupation'] = 'Engineer'

# Removing a key-value pair using del keyword
del my_dict['city']

# Printing the modified dictionary
print(my_dict)

In the above code snippet, we add a new key-value pair ‘occupation’: ‘Engineer’ using assignment. Then, we remove the key-value pair with key ‘city’ using the del keyword. Finally, we print out the modified dictionary.

Iterating Over a Dictionary

You can iterate over a dictionary using loops such as for loop. Let’s see an example:

# Iterating over keys and values
for key, value in my_dict.items():
    print(key, ':', value)
    
# Output:
name : John
age : 26
occupation : Engineer

In the above code snippet, we are using the items() method to iterate over the key-value pairs in the dictionary. We then print out each key and value on separate lines.

Conclusion

In Python, a data type called mapping, or more specifically, a dictionary, is used to store and manipulate collections of key-value pairs. With dictionaries, you can easily access, modify, add, and remove elements using their corresponding keys. Additionally, dictionaries allow for easy iteration over their contents.

Dictionaries are an essential part of Python programming and can be utilized in a wide range of applications. By understanding how to create, access, modify, and iterate over dictionaries, you will have a powerful tool at your disposal for managing and organizing data efficiently within your Python programs.

Discord Server - Web Server - Private Server - DNS Server - Object-Oriented Programming - Scripting - Data Types - Data Structures

Privacy Policy