Starting our Journey with Dictionaries

We have boarded the plane and embarked on our journey into the world of Python. Having explored the terrain of lists and tuples, it's now time for us to venture into the bustling cities of dictionaries. Please fasten your seatbelts, as we dive deeper into this fascinating landscape!

Welcoming Dictionaries

Suppose you're traveling and have to remember the capitals of numerous countries. You could remember each one individually, but wouldn't it be more efficient if you had a map where each country leads to its capital? That's what dictionaries in Python are like: they're comparable to compact maps where each key (such as country names) unlocks a value (like their capitals).

Dictionaries can be thought of as sets of key:value pairs. Any immutable type can be a key. A key is unique within a single dictionary. In Python, an empty dictionary is represented by a pair of braces {}. To create a dictionary with starting key:value pairs:

Unlike sequences, dictionaries are indexed by their keys. So, in the above example, to get the capital city of France you would use "France" as the key like so: capital_cities["France"].

Update existing values or store new ones by assigning the value to a key:

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal