Junior — Middle
Can you explain what the built-in data structure 'map' in Python is?
sobes.tech AI
Answer from AI
In Python, there is no built-in data structure called map as a separate type, but there is a function map(), which is used to apply a function to each element of an iterable.
If we talk about a data structure similar to "map", then it is a dictionary (dict). A dictionary is a collection of key-value pairs, where keys are unique and used for quick access to the corresponding values.
Example of using a dictionary:
# Creating a dictionary
my_dict = {'apple': 3, 'banana': 5, 'orange': 2}
# Accessing a value by key
print(my_dict['banana']) # Outputs: 5
# Adding a new element
my_dict['pear'] = 4
# Iterating over keys and values
for key, value in my_dict.items():
print(f'{key}: {value}')
Thus, a dictionary in Python is a data structure that implements the "map" (mapping) concept from other languages.