Description
This paper is meant to help readers get familiar with the concepts of dictionaries in python and the methods that go along with them.
Defining a dictionary:
#A dictionary that maps players of a video
#game to their respective high scores
high_scores={"Utsav": 234, "Siddarth": 898}
- dictionaries are contained in curly brackets:
{}
Using dictionaries to represent complex data structures
Hash map:
One of the most common ways dictionaries in python are used is as a hash map
Hash maps are used to map certain keys to their corresponding values, they're usually used as a way to reduce time complexities of algorithms, However there's a trade-off because they end up increasing the space complexity of the code.
Graphs (Adjacency Lists):
Dictionaries are also used to store Adjacency lists, which are essentially a way to represent graphs, It maps a key to a list of values, where a key represents a node and the subsequent list of values represent all the connected nodes.
graph={ 1: [2,3,4] ,
2 : [ 6,7 ]
}
Adding an element in a dictionary
Suppose we have to increment the value for 1
in the given dictionary
frequency={}
if 1 not in frequency:
frequency[1]=1
else:
frequency[1]+=1
Iterating through a dictionary
The intuitive way
for i in dictionary:
print(i, dictionary[i])
- The way listed above prints the keys and their corresponding values, It's simple and easy to understand especially for people from non python backgrounds.
The pythonic way
for keys,values in dictionary.items():
print(keys, values)
- The
.items()
function returns a tuple of two elements:(keys, values)
. This way is easier on the eyes and looks much more readable. This way of iterating is also in line with the PEP8 standards.
Sorting a dictionary
Dictionaries do not have a .sort()
function like lists do in python, but fear not! The gods of programming are not that merciless.
The sorted()
function essentially works the same way. However, make sure you pass the dictionaries using the .items()
method.
dictionary={1:3,2:2,3:1}
print(sorted(dictionary))
#The code above will sort and print
#The list of keys by default
#output: [1,2,3]
print(sorted(dictionary.items))
#Now that we're passing both items
#output: {1:3,2:2,3:1}
- But what if you want to sort this dictionary by it's values instead ? Let's take a short detour to lambda functions
fun= lambda x:x+5
#To easiest way to explain this is to
#show you how you can achieve this
#without lambda
def fun(x):
return x+5
- These functions save us from needing to type out small functions that will never be used again, do not use lambda if you need to type it out a lot of times in your code, Make it a separate function instead. Now let's get back to where we were:
dictionary={1:3,2:2,3:1}
ans=sorted(dictionary.items(), key= lamda x: x[1])
print(ans) #output: {3:1,2:2,1:3}
- since
.items()
returns a tuple of key value pairs, specifyingx[1]
notifies the function that the dictionary needs to be values.
Miscellaneous functions
.update()
: This function is used in between dictionaries, let's say you have a current_dictionary
and an updated_dictionary
, if you want to update the values in your current dictionary using the corresponding keys in the new dictionary, you can use this function.
current_dictionary.update(new_dictionary)
.pop()
: This function removes elements from a dictionary based on the keys provided.
age={"utsav":23, "siddarth":6}
age.pop("siddarth")
print(age) #output: {"utsav":23}
- The function above removes items in dictionary based on their keys
Top comments (0)