Few mistakes when using Python dictionary

PythonServer Side ProgrammingProgramming

Dictionaries in python are a type of data structure that map keys to values as a key-value pair. They are one of the frequently used data structures and have many interesting properties. They are presented by enclosing them in a pair of curly brace like below.

dict = {'day1':'Mon' ,'day2':'Tue','day3':'Wed'}

The elements or key-value pairs in the dictionary are represented within single quotes and separated by a colon.

Creating a Dictionary

We create a dictionary by assigning values written in the form of a ke.

Example

Dict1 = {'day1':'Mon' ,'day2':'Tue','day3':'Wed'}
print(type(dict1))
print(dict1)

# Using the dict() method
dict2 =dict({('day1','Mon'),('day2','Tue'),('day3','Wed')})
print(type(dict2))
print(dict2)

Output

Running the above code gives us the following result −

{'day1': 'Mon', 'day2': 'Tue', 'day3': 'Wed'}

Accessing Values in Dictionary

To access the elements of a dictionary, we can use the square brackets along with the key to obtain its value. We can also use the get() method to get the values for the dictionary elements.

Example

dict = {'day1': 'Mon', 'day2': 'Tue', 'day3': 'Wed','weekend':{'d1':'Saturday','d2':'Sunday'}}
print(dict['day2'])
print(dict['weekend'])
print(dict.get('day3'))

Output

Running the above code gives us the following result −

Tue
{'d1': 'Saturday', 'd2': 'Sunday'}
Wed

Adding elements to a Dictionary

We add new elements to a dictionary by adding a new key-value pair. We can also add another dictionary as an element to create a nested dictionary.

Example

dict = {'day1': 'Mon', 'day2': 'Tue', 'day3': 'Wed'}
dict['day4']='Thu'
dict['day5']='Fri'
print(dict)

Output

Running the above code gives us the following result −

{'day1': 'Mon', 'day2': 'Tue', 'day3': 'Wed', 'day4': 'Thu', 'day5': 'Fri'}

Updating Dictionary

We can update a dictionary by adding a new entry or a key-value pair and modifying an existing entry. We have already seen the addition of new elements to the dictionary above. Now we will see the modification of existing entry. Here we simply take the key and assign the new value to the element.

Example

dict = {'day1': 'Mon', 'day2': 'Tue', 'day3': 'Wed'}
dict['day1']='Monday'
dict['day2']='Tuesday'
print(dict)

Output

Running the above code gives us the following result −

{'day1': 'Monday', 'day2': 'Tuesday', 'day3': 'Wed'}

Delete Elements of a Dictionary

The specific elements of a dictionary can be deleted by using the del keyword. It can also be used to delete the entire dictionary. There is also the clear() method which can be used to remove elements form the entire dictionary.

Example

dict = {'day1': 'Mon', 'day2': 'Tue', 'day3': 'Wed'}
print(dict)
del dict['day3']
print(dict)
dict.clear()
print(dict)

Output

Running the above code gives us the following result −

{'day1': 'Mon', 'day2': 'Tue', 'day3': 'Wed'}
{'day1': 'Mon', 'day2': 'Tue'}
{}

Some common mistakes while using dictionaries in python

  • Using dict_name[Key_value] to access the values ay not work correctly always and throw exceptions. SO the get() method is a better method to use for accessign the elements of a dictionary.

  • Similarly use the update(key=value) method to update the values in a dictionary isntead of using the direct dict_name[key_value]='new_value' approach.

  • Creating a copy of a dictionary by using dict1=dict2 will create two dictionaries but both refer to the same object. SO both will get updated at the same time. Use shallow copy if you want to avoid updating both the copies.

  • To go through the elements of the dictionary in a specific order use the sorted() method.

  • Take a judgement on when to use the dictionaries and not to use because there are other data structures like tuples, sets etc which may fit better into the requirement.

raja
Published on 17-Oct-2019 16:10:00
Advertisements