<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: jamessidis</title>
    <description>The latest articles on DEV Community by jamessidis (@jamessidis).</description>
    <link>https://dev.to/jamessidis</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F547963%2F31bdbdcf-cfab-423c-b6e6-7028f4ebc558.jpeg</url>
      <title>DEV Community: jamessidis</title>
      <link>https://dev.to/jamessidis</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jamessidis"/>
    <language>en</language>
    <item>
      <title>Python Dictionary Comprehensions with Examples </title>
      <dc:creator>jamessidis</dc:creator>
      <pubDate>Sun, 27 Dec 2020 13:11:14 +0000</pubDate>
      <link>https://dev.to/jamessidis/python-dictionary-comprehensions-with-examples-41d1</link>
      <guid>https://dev.to/jamessidis/python-dictionary-comprehensions-with-examples-41d1</guid>
      <description>&lt;p&gt;In this tutorial, I am going to talk about Python Dictionary Comprehension. &lt;/p&gt;

&lt;p&gt;If you want to learn more about Python Dictionary, you can look at my free Python Tutorial. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://learnandmakeit.com/python-tutorial/"&gt;Python Tutorial&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://learnandmakeit.com/dictionaries/"&gt;Dictionaries in Python&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;First, I am going to talk about what is Python Dictionary. Then I am going to talk about Python Dictionary Comprehension with examples.&lt;/p&gt;

&lt;h1&gt;
  
  
  Python Dictionary
&lt;/h1&gt;

&lt;p&gt;Dictionary is an ordered, changeable, and indexed collection of items. Dictionaries have &lt;strong&gt;key&lt;/strong&gt; and &lt;strong&gt;value&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;With dictionary, you can do many tasks that require a lot of code with a single line of code.&lt;/p&gt;

&lt;p&gt;Let’s give an example of using a dictionary in real life.&lt;/p&gt;

&lt;p&gt;You don’t know the phone number of everyone in your phone book. You save the number with the name of the person so that you can access and call the person's number with the name at any time.&lt;/p&gt;

&lt;p&gt;This is also the same for the python dictionary. Python also consists of dictionary key and value items. We use &lt;strong&gt;curly brackets&lt;/strong&gt; to create a dictionary in Python.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Python dictionary syntax structure is as follows&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my_dictionary = { "key1": value1, "key2": value2 } 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, Let’s learn about dictionary comprehension in Python.&lt;/p&gt;

&lt;h1&gt;
  
  
  Python Dictionary Comprehension
&lt;/h1&gt;

&lt;p&gt;Dictionary comprehension is a special and elegant way to create dictionaries.&lt;/p&gt;

&lt;p&gt;We can create dictionaries using simple expressions and dictionary comprehension should be written in a specific pattern.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The syntax of dictionary comprehension&lt;/strong&gt; :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{ key : value for ( key, value ) in dictionary.items() }  
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Why Use Dictionary Comprehension
&lt;/h1&gt;

&lt;p&gt;First of all, Let’s start by explaining why we use comprehension.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt; : &lt;/p&gt;

&lt;p&gt;Without comprehension:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;values = [] # empty list

for x in range(5):
values.append(x * 2)

print(values)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt; :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[0, 2, 4, 6, 8] 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With Comprehension:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;values = {x: x * 2 for x in range(5)}

print(values) 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt; :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{0: 0, 1: 2, 2: 4, 3: 6, 4: 8} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see we can do the same program with just a line of code with comprehension.&lt;/p&gt;

&lt;p&gt;Now, Let’s look at other dictionary comprehension examples.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt; : &lt;/p&gt;

&lt;p&gt;Let say that we want to change the product prices from dollar to euro. Here is the program with dictionary comprehension.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my_dictionary = {"computer": 900, "phone": 400, "book": 10}
dollar_to_euro = 0.84

new_price = {key: value * dollar_to_euro for(key, value) in my_dictionary.items()}

print(new_price) 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt; :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{'computer': 756.0, 'phone': 336.0, 'book': 8.4} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Using Conditional in Python Dictionary
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Python Dictionary If Condition
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt; :&lt;/p&gt;

&lt;p&gt;In this example, if the person’s age bigger or equal to 18, we will add it to our new dictionary.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my_dictionary = {"James": 25, "John": 17, "Adam": 36}

new_dictionary = {k : v for (k,v) in my_dictionary.items() if v &amp;gt;= 18}

print(new_dictionary) 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt; :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{'James': 25, 'Adam': 36} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Python Dictionary Multiple Condition
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt; :&lt;/p&gt;

&lt;p&gt;In the same example if the person’s age is bigger or equal to 18 and divided by 2, then we will add it to our new dictionary.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my_dictionary = {"James": 25, "John": 17, "Adam": 36}

new_dictionary = {k : v for (k,v) in my_dictionary.items() if v % 2 == 0 if v &amp;gt;= 18}

print(new_dictionary) 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt; :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{'Adam': 36}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Python Dictionary Comprehension If Else Condition
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt; : &lt;/p&gt;

&lt;p&gt;In the same example, if the person’s age is bigger or equal to 18, our program will add it as ” &lt;strong&gt;can vote&lt;/strong&gt; ” otherwise “&lt;strong&gt;cannot vote&lt;/strong&gt;“.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my_dictionary = {"James": 25, "John": 17, "Adam": 36}

new_dictionary = {k : ('can vote' if v &amp;gt;=18 else 'cannot vote') for (k,v) in my_dictionary.items() }

print(new_dictionary)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt; :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{'James': 'can vote', 'John': 'cannot vote', 'Adam': 'can vote'} 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see we can write better, cleaner code with dictionary comprehension. In short, This is more Pythonic.&lt;/p&gt;

</description>
      <category>python</category>
    </item>
  </channel>
</rss>
