DEV Community

loading...

All about Python Dictionaries

Pritpal Singh
・2 min read

Python Program to understand dictionaries.

1. Here I am creating a dictionary.

my_dict1 = {}   
Enter fullscreen mode Exit fullscreen mode

using {} for declaring an empty dictionary as dictionaries are declared using {}.

my_dict2 = dict()
Enter fullscreen mode Exit fullscreen mode

using dict() is also a way to declare an empty dictionary.

2. In dictionary we use pairs of key:values.

my_dict1 = {'fruit' : 'apple', 'drink' : "pepsi"}
Enter fullscreen mode Exit fullscreen mode

here we used fruit and drink as a key and apple and pepsi as their values resp.
we can't have two or more keys with same name.

my_dict2 = {'vegetable' : 'carrot', 'fruit' : my_dict1} 
Enter fullscreen mode Exit fullscreen mode

here we are declaring a nested dictionary.

3. We can add and update elements in dictionay using a no. of methods. Use [] for accesing a key or adding and updating.

my_dict2['fruit'] =  'orange' 
Enter fullscreen mode Exit fullscreen mode

If we use a key which already exist then it updates the previous value.

my_dict2['snacks'] = 'chips', 'cookies' 
Enter fullscreen mode Exit fullscreen mode

we are assigning a no. of values to a key and it get stored in the form of tuple(or we can cast it)

my_dict2.update({'snacks' : 'chips'}) 
Enter fullscreen mode Exit fullscreen mode

here we are updating the existing key and if key doesn't exist then it will add new key.

4. We can access a value inside dictionary using the key associated to it inside a [] and we can also use get method.

print(my_dict1['fruit'])
Enter fullscreen mode Exit fullscreen mode

If key is not present in dictionary then it will give error.

print(type(my_dict1.get('snacks')))
Enter fullscreen mode Exit fullscreen mode

the difference here is, in get(value, default='None') method will return none if key is not present in dictionary.
we can use two arguments in get(). It will return the second value if key is not present.

5. We can remove a key by using the del/clear/pop method.

del my_dict2["fruit"]   
Enter fullscreen mode Exit fullscreen mode

here we are deleting a item from the dictionary

my_dict2.pop('vegetable')   
Enter fullscreen mode Exit fullscreen mode

here we are using pop(key, default = 'None') method to remove a item from dictionary and return the value associated with key. If item not present in dictionary then it return the second argument.

my_dict2.popitem()  
Enter fullscreen mode Exit fullscreen mode

this method will remove the last inserted item in the dictionary and return it as a tuple.

my_dict2.clear()    
Enter fullscreen mode Exit fullscreen mode

this method will clear the dictionary.

del my_dict2    
Enter fullscreen mode Exit fullscreen mode

this will delete the dictionary.

6. Methods on Dictionaries.

keysl = my_dict1.keys()  
Enter fullscreen mode Exit fullscreen mode

it returns a list of keys.

values1 = my_dict1.values() 
Enter fullscreen mode Exit fullscreen mode

it returns a list of values in dictionary.

items1 = my_dict1.items()   
Enter fullscreen mode Exit fullscreen mode

it returns list of a given dictionary’s (key, value) tuple pair.

my_dict2=my_dict1.copy()    
Enter fullscreen mode Exit fullscreen mode

it copies my_dict1 into my_dict2

my_dict3 = dict(my_dict1)   
Enter fullscreen mode Exit fullscreen mode

this is also a way to copy a dictionary into another

below method returns a dictionary with the specified keys and the specified value(dictionary.fromkeys(keys,value))

my_dict2 = my_dict3.fromkeys('1', 'none') 
print(my_dict2)
Enter fullscreen mode Exit fullscreen mode
dict_comb=my_dict1 | my_dict2   
Enter fullscreen mode Exit fullscreen mode

here we are combinig two dictionary

dict_comb2={**my_dict1, **my_dict2} 
Enter fullscreen mode Exit fullscreen mode

Using ** we first pass all the elements of the first dictionary into the third one and then pass the second dictionary into the third.

Discussion (0)