Python For Beginners

Python For Beginners

Python Dictionary

What is Python Dictionary?

Python dictionary is one very useful collection type.

Python’s dictionaries allow you to connect pieces of related information, or even more to combine into a same group.

Dictionaries are key-value pairs, meaning when you provide a key, Python will return the value associated with that key.

How to make a Python Dictionary?

To define a Python Dictionary, we use curly braces { }.

To connect keys and values we use colons : and we use commas  , to separate individual key-value pairs.

car = { 'type': 'Audi', 'color': 'black', 'year': 2017 }

This is an example of dictionary. We used this dictionary to describe car, with his type, color and year.

car = { 
   'type': 'Audi', 
   'color': 'black', 
   'year': 2017 
}

Sometimes, we might make a same dictionary in multiple lines, to improve visibility. This does not change anything, it is simply to make our code prettier.

How to access Python Dictionary values?

car = { 
    'type': 'Audi', 
    'color': 'black', 
    'year': 2017
}

print(car['type']) 
print(car['color'])

We can access dictionary values by assigning the key to the name of the dictionary, and placing that key in square brackets [].

If the key you’re asking for is not in the dictionary, an error will occur.

You can also use the get() method, which returns None instead of an error if the key doesn’t exist.

You can also specify a default value to use if the key is not in the dictionary.

car = { 
    'type': 'Audi', 
    'color': 'black', 
    'year': 2017
}

car_type = car.get('type')
car_color = car.get('color')
car_owner = car.get('owner', 'unknown')

print(car_type)
print(car_color)
print(car_owner)

In the example above, we are using get() function to get values from our indexes. Notice that we are also using  get('owner','unknown') which means “If the owner of car is not defined, just return unknown”.

How to add new key-value pair to a dictionary?

We can store as many key-value pairs as we want in a dictionary. The only limit here is computer memory (which will not be so easily used by simple text data in dictionary).

To add a new key-value pair to an existing dictionary we give the name of the dictionary and the new key in square brackets, and set it equal to the new value.

Lots of times, in real life programs, we are starting with an empty dictionary, which we later ‘populate’ with some data from a database or user input.

car = {} 

car['type'] = 'Audi' 
car['color'] = 'black'
car['year'] = 2017

print(car)

This is an example where we have empty dictionary car and we assign type, color and year to it.

car = { 'type': 'Audi', 'color': 'black', 'year': 2017 }

car['owner'] = 'John' 
car['last_registration_date'] = '2019-01-07' 

print(car)

This is an example where we have already existing and populated dictionary car, but we assign some more attributes (key-values) to it.

How to modify values in dictionary?

How to remove key-value pairs from dictionary?

How to determine a dictionary length?

How to loop trough a dictionary? How to get all the values from a dictionary?

 

Leave a Comment