Master AI & Build your First Coding Portfolio with SkillReactor | Sign Up Now

Lesson 5 - Basic Data Structures

5.3 Dictionaries

Dictionaries in Python are an unordered collection of key-value pairs, enclosed by {}. Each key in a dictionary is unique and associated with a corresponding value.

For example, consider a mark sheet where subjects are listed along with their respective marks. This can be represented as a dictionary where the subject names serve as keys and the marks as values.

Here's an example of a simple dictionary:

my_dict = {'name': 'Alex', 'age': 25, 'city': 'New York'}

Unlike lists and tuples, dictionaries require you to use the key to retrieve specific items. Take a look at the following example:

my_dict = {'name': 'Alex', 'age': 25, 'city': 'New York'}
print(my_dict['age'])

In this example, we used the key 'age' to access the corresponding value '25'. Try the same example using the key 'name' to access the value 'Alex'.