Python Concept for Data Science : Dictionaries
What are Python Dictionaries?
Dictionary is a built-in Python Data Structure that is mutable. It is similar to List, Set, and Tuples. However, it is not indexed by a sequence of numbers but indexed based on keys and can be understood as associative arrays. It consists of a key with an associated value.
How to Create a Dictionary in Python?
A dictionary is represented by a pair of curly braces {} in which enclosed are the key: value pairs separated by a comma.
Let's look at the syntax of a dictionary: dictionary = {"key_1": "value_1", "key_2": "value_2", "key_3": "value_3"}
Example:
my_dict1 = {'Fruit': 'Apple', 'Vegetable' : 'Potato', 'Food' : 'Fried Chicken'}
print(my_dict1)
The output is:
{'Fruit' : 'Apple', 'Vegetable' : 'Potato', 'Food' : 'Fried Chicken'}
Make a Dictionary with dict() command:
Using the dict() function, you can convert a compatible combination of constructs into a Python dictionary. Let’s take an example for converting a list into the dictionary with dict() command:
# first create the list
list_1 = ([1,