The ultimate guide to Python Dictionary comprehension

Posted on July 21st, 2021

Python dictionaries — or dict in Python — is a way of storing elements similar to a Python list. However, rather than accessing elements through an index, accessibility is achieved through a pre-assigned fixed key. In a way, a dictionary is like a mini on-site database that can be queried using a “key-value” pair. This method of structuring data lets you create more complex structures and may be more suitable to your needs than a simple, flat, and potentially mutable list.

Dictionaries are something that comes up in spaces like data science, due to their ability to hold multiple dimensions and nested data. In this tutorial, we will go over the following:

  • what a Python dictionary is and how to effectively use it
  • what Python dictionary comprehension is and why it is an important alternative to loops and lambdas

Without further ado, let’s get started.

What is a Python dictionary and how to use one

A Python dictionary is a collection of items that’s accessible by a specific key rather than an index. This key can be one of the following types:

  • integer
  • floating point
  • number
  • string
  • tuple
  • fronzensets

This is because a Python dictionary key needs to be hashable. Hashing is a process of putting a specific variable through a type of function called a “hash function” to return a unique output.

Here is an example of how to initialize a Python dictionary:

 a = {'Luxembourg': 'Western Europe', 'Norway': 'Northern Europe', 'United States': 'North America', 'Ireland': 'Northern Europe'}

To add to the Python dictionary:

 a['Denmark'] = 'Northern Europe'

Now, if we print it out:

 print(a['Luxembourg'])

This will return:

 Western Europe

However, if it was a list, we’d use the following syntax:

 print(a[0])

But this will not work and will return a KeyError. This is because the key 0 doesn’t exist. Querying Python dictionaries is done through the key rather than an index.

Each item in the dictionary can have any data type and isn’t confined to a string as per the example above. It is also good to note that the key has to be unique in a Python dictionary. This means that no duplicates are allowed. However, if there are duplicate keys, Python will not throw an error. Instead, the last instance of the key will be the valid value.

For example:

 a = {'Luxembourg': 'Western Europe', 'Norway': 'Northern Europe', 'United States': 'North America', 'Ireland': 'Northern Europe', 'Luxembourg': 'Luxembourg City'}

So when you run print(a['Luxembourg']), the returned result will be Luxembourg City rather than Western Europe.

And that’s basically creating a Python dictionary in a nutshell.

What is Python Dictionary Comprehension?

Dictionary comprehension is a process of transforming one dictionary into another. The items in the original dictionary can be conditionally included and each item transformed as necessary.

In a way, it’s like copying your original dictionary and doing what you need to do with the values before your final dictionary is set. To do this, you will need to be able to access the key and the value within each dictionary object. This is achievable using .keys() and .values().

.keys() will return a list of keys contained in your dictionary. For example:

 a = {'Luxembourg': 'Western Europe', 'Norway': 'Northern Europe', 'United States': 'North America', 'Ireland': 'Northern Europe'}
 ​
 a.keys()

This will return:

 dict_keys(['Luxembourg', 'Norway', 'United States', 'Ireland'])

The same is applied to .values(). Here is an example of what it looks like:

 a.values()

This will return:

 dict_values(['Western Europe', 'Northern Europe', 'North America', 'Northern Europe'])

Now that you’ve created these lists, you can now pair them up using items(). This will give you access to each key-value pair. Here is an example:

 a.items()

This will return:

 dict_items([('Luxembourg','Western Europe'), ('Norway','Northern Europe'), ('United States','North America'), ('Ireland','Northern Europe')])

Using keys(), values() and items() sets you up with the ability to create dictionary comprehensions in Python. The general syntax structure for creating a new dictionary is as follows:

 new_dictionary_name_here = {key: value for (key,value) in origianl_dictionary.items()}

For a direct copy of the dictionary through Python dictionary comprehension, here is an example of the syntax:

 a_copy = {k:v for (k,v) in a.items()}

The first v is representative of any transformations you may want your values to be. You can also make transformations to the key as well by modifying the first instance of the k.

For example:

 alpha_dictionary = {'z': 26, 'y': 25, 'x': 24, 'w': 23}
 double_dictionary = {k*2:v*2 for (k,v) in alpha_dictionary.items()}

So when we use print(double_dictionary), we will get the following results:

 {'zz': 52, 'yy': 50, 'xx': 48, 'ww': 46}

Why Use Dictionary Comprehension?

As shown in the process above, we don’t have to loop or use lambda functions in order to create copies of a dictionary. During the copying process, dictionary comprehension also lets us create transformations easily.

The thing with loops is that they are used to repeat a block of instructions for a given number of iterations. But things can get complicated for nested loops, which can create confusion and unnecessary complexity.

Dictionary comprehensions are a better alternative to writing loops due to simplification of the ‘looping’ process and therefore readability. For example, rather than writing something like this:

 alpha_dictionary = {'z': 26, 'y': 25, 'x': 24, 'w': 23}
 double_dictionary = {}
 ​
 # Add values to `alpha_dictionary` using for loop
 for n in numbers:
     if n%2==0:
         new_dict_for[n] = n**2
 ​
 print(double_dictionary)

You can simply write the following using Python dictionary comprehension:

 alpha_dictionary = {'z': 26, 'y': 25, 'x': 24, 'w': 23}
 ​
 double_dictionary = {n:n**2 for n in numbers if n%2 == 0}
 ​
 print(double_dictionary)

Python dictionary comprehension is also a good alternative to lambda functions. A lambda function is a way of creating small anonymous functions. They are considered throwaway functions and are nameless. Their existence only lasts as long as they are needed. lambda functions tend to also include other functions such as filter(), map(), and reduce() to achieve its desired outcome.

For example, let’s create a new dictionary using lambda and map() function.

 fahrenheit = {'t1':0, 't2':10, 't3':20, 't4':30}
 ​
 celsius = list(map(lambda x: (float(5)/9)*(x-32), fahrenheit.values()))
 ​
 celsius_dict = dict(zip(fahrenheit.keys(), celsius))

However, converting Fahrenheit to Celsius is much simpler using a dictionary comprehension. Here is the refactored code:

 fahrenheit = {'t1':0, 't2':10, 't3':20, 't4':30}
 celsius = {k:(float(5)/9)*(v-32) for (k,v) in fahrenheit.items()}

Overall, there are less lines involved with a dictionary comprehension and informs the developer exactly what the expected end result is to be — another dictionary with the values translated and transformed from the original dictionary.

This is the barebones basics of python dictionary comprehension. The next step is to explore conditions, such as if conditions and using multiple if conditions. Beyond this is nested dictionary comprehension — which poses its challenges due to the nested nature. This is where loops can come into play and help.

Overall, dictionary comprehension is the easiest way to work with sets of data concisely and succinctly. It’s not hard once you get your head around the idea and the techniques above are more than enough to get you started.

AI-assisted software development just got bigger: Welcome, Copilot!

Posted on July 13th, 2021

Artificial Intelligence is rapidly changing nearly all aspects of life and business, and software development is no exception. We started Tabnine with a single goal in mind, to put the power of AI in the hands of developers and teams. Our top priority since day one has been to help developers become better with intelligent tools that fit seamlessly into their workflow. The rapid adoption of Tabnine and the consistently enthusiastic feedback from developers has already made it clear that we’re onto something huge.

The announcement of GitHub Copilot marks a milestone for AI-Assisted Software Development in general and for Tabnine in particular. As pioneers in the AI-Assisted Software Development space, we’re excited to see giants like Microsoft, OpenAI, and GitHub recognize this incredible opportunity.

At Tabnine, we believe in putting the developer at the center, using AI as smart tooling that augments and assists developers, making them better. We will continue to prioritize making developers successful above anything else. In particular, we’ll invest in key factors that we believe make Tabnine so appealing to developers using all IDEs and programming languages:

  • Accuracy and usability: We’re making Tabnine’s AI even stronger and our UX more streamlined. Our priority is to assist and accelerate development while keeping developers in full control, avoiding the heavy mental load of evaluating large code blocks with insufficient context. We optimize the user experience by favoring bite-size suggestions making sure that developers can make snap judgments about the relevance of the suggestions with minimal effort.
  • Personalization – learning from you & your team: Uniquely, Tabnine learns the specific code patterns used by you and your team (locally and privately) and tailors code suggestions to your project. We will amplify our value for teams, helping them take team productivity and code consistency to the next level.
  • Privacy & compliance: Recognizing that many professional developers cannot send code to query a cloud prediction service, Tabnine offers an unparalleled option for running the AI model locally on a developer’s machine (without sending code to the cloud). Moreover, we guarantee that Tabnine doesn’t learn from developer interactions to train the global model, keeping everything that Tabnine learns from you at your control and available only to authorized team members.

I am certain that AI will play an incredibly important role in modern software development. At Tabnine we’re proud to be a part of bringing this future to life and leveraging the amazing technology efficiently and responsibly for the benefit of developers and teams.