Python is a high-level programming language that is widely used for developing various applications. One of the most important data structures in Python is the dictionary. A dictionary is an unordered collection of key-value pairs, where each key is unique and maps to a corresponding value. In this article, we will discuss Python dictionaries in detail.
A dictionary in Python is created using curly braces {} and key-value pairs separated by a colon. For example:
my_dict = {'name': 'John', 'age': 25, 'city': 'New York'}
In the above example, 'name', 'age', and 'city' are the keys, and 'John', 25, and 'New York' are the corresponding values. We can access the values of a dictionary using the keys. For example:
print(my_dict['name']) # Output: John
We can also add new key-value pairs to a dictionary using the following syntax:
my_dict['occupation'] = 'Engineer'
print(my_dict) # Output: {'name': 'John', 'age': 25, 'city': 'New York', 'occupation': 'Engineer'}
We can remove a key-value pair from a dictionary using the del keyword. For example:
del my_dict['city']
print(my_dict) # Output: {'name': 'John', 'age': 25, 'occupation': 'Engineer'}
We can also check if a key exists in a dictionary using the in keyword. For example:
if 'name' in my_dict:
print('Name is present in the dictionary')
else:
print('Name is not present in the dictionary')
Output: Name is present in the dictionary
Let's look at some code examples to understand Python dictionaries better:
text = "Python is a high-level programming language that is widely used for developing various applications"
word_freq = {}
for word in text.split():
if word in word_freq:
word_freq[word] += 1
else:
word_freq[word] = 1
print(word_freq)
In the above example, we are counting the frequency of each word in the given string. We are creating an empty dictionary word_freq and iterating over each word in the string. If the word is already present in the dictionary, we increment its count by 1. Otherwise, we add the word to the dictionary with a count of 1. The output of the above code will be:
{'Python': 1, 'is': 2, 'a': 1, 'high-level': 1, 'programming': 1, 'language': 1, 'that': 1, 'widely': 1, 'used': 1, 'for': 1, 'developing': 1, 'various': 1, 'applications': 1}
keys = ['name', 'age', 'city']
values = ['John', 25, 'New York']
my_dict = dict(zip(keys, values))
print(my_dict)
In the above example, we are creating a dictionary from two lists - keys and values. We are using the zip function to combine the two lists into a list of tuples, and then passing this list to the dict function to create a dictionary. The output of the above code will be:
{'name': 'John', 'age': 25, 'city': 'New York'}
my_dict = {'person1': {'name': 'John', 'age': 25}, 'person2': {'name': 'Jane', 'age': 30}}
print(my_dict['person1']['name']) # Output: John
print(my_dict['person2']['age']) # Output: 30
In the above example, we are creating a dictionary with two nested dictionaries. We can access the values of the nested dictionaries using the keys. The output of the above code will be:
John
30
Python dictionaries are a powerful data structure that allows us to store and manipulate key-value pairs. We can use dictionaries to solve a wide range of problems in Python programming. In this article, we discussed the basics of Python dictionaries and looked at some code examples to understand them better.