Dictionary & Set Comprehensions

Deepti Goyal
Analytics Vidhya
Published in
5 min readApr 9, 2020

Dictionary comprehensions are very similar to list comprehensions- but instead of values, we have key-pair values. If you have not read my last blog on List comprehensions. I would advise you to please have a look at it before we jump to dictionary comprehensions

Link to List Comprehensions — https://medium.com/analytics-vidhya/list-comprehensions-9de2f2752c72?source=friends_link&sk=6f45c55e9af6eee9da0c1fa976f3bd0f

Looking at earlier examples of squares of numbers ranging from 0–8 ( similar to what we did in lists). Let’s print the square of numbers using a Dictionary like this — square = {1:1,2:4….}

This is the very basic method —

Note: since the right element is excluded in the range, so in order to get the squares from 0 till 8 we have to give the range till 9.

Square of numbers from 0–8

How do we solve the above using Dictionary comprehension
(Similar to what we did in the list comprehensions -)

First, we write the output we want (in this case we want x**2 ) followed by the for loop.

Square of numbers from 0–8

If you want to print them in separate lines- just run a for loop as shown below

Above output printed in separate lines

Let’s make it little pretty — how about we add an ‘f’ string to it -

This output looks much better now :)

Let's look into other examples-

The most common example and important question for the interview also — ***
Count the occurrence of characters in the string using dictionary —
so for this, we need to know some basic string functions such as — function to count character in a string is — string.count(char)
Let’s say we have a string1 — ‘sunny’ and we want to count the number of times character occurred in the string.

This is how we do it using Dictionary

Counting unique characters occurrence in the string

How do we solve the above example using dictionary comprehensions

Counting unique characters occurrence in the string

What if instead of a word we have a sentence and we want to count the occurrence of each word in that string -

Let’s convert our string to a list using the split() method. If you need a detailed explanation of string operations and functions. please let me know in the comment section. I will post a blog related to that.

Converting a string to a list using split() method-

Now Let’s create an empty dictionary string2 = {} and then implement a for loop on that list to get the count of each word as shown below —

let’s see how do we solve the above problem statement using Dictionary comprehension -

Steps —

we first write what we want as an output inside curly braces(since it is a dictionary). In this case, we want our output to look like ‘dictionary’:1.. and so on.. (key: value pair) here ‘dictionary’ is our key and 1 is our value-

As we know in order to get the count of the string we have a function string.count(char) so using this function we can write our output as keys:string1.count(keys) followed by the for loop for keys in string1

If-Else in Dictionary comprehensions

Let’s say we want a dictionary which contains the square of even numbers and cube of odd numbers ranging from 0–10
How do we implement this using Dictionary

If you want the output in separate lines — implement a for loop as shown below —

Let’s see how do we implement this using Dictionary comprehension -

Let’s print them in separate lines using for loop -

Great, we are done with dictionary comprehension.

Let’s move to our next section —

Sets Comprehension

Let’s see how can we use set comprehension — Although it is rarely used in the coding, it is always good to know :)
As we all know that set is an unordered collection of unique items so there is no fixed order defined — output can be printed in any order
It is similar to what we have been doing with the list and dictionary. Let’s move to its implementation directly
For example —
If we want to print the squares of the numbers ranging from 0,11 then -this is how we do in Sets Comprehension-

As we can see the output we get is not in the order — because Set is an unordered collection of unique items. You might get different answers while running it.
In the dictionary, we specify both key and value but in Sets, we just specify the value.

let’s look into square_cube example mentioned above

I hope you all enjoyed this tutorial on Sets and Dictionary comprehension.

You can find a detailed explanation of List comprehension here.

Keep Learning. Stay Safe. My Github link is —

LinkedIn- https://www.linkedin.com/in/deepti-aggarwal/

Note: — Images taken from Clipart library

--

--