<?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: Rakesh Gautam</title>
    <description>The latest articles on DEV Community by Rakesh Gautam (@rkshrksh).</description>
    <link>https://dev.to/rkshrksh</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%2F241518%2Fb4b3df2d-4aba-40fa-a731-92c77c9b251b.png</url>
      <title>DEV Community: Rakesh Gautam</title>
      <link>https://dev.to/rkshrksh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rkshrksh"/>
    <language>en</language>
    <item>
      <title>Using Defaultdict in Python</title>
      <dc:creator>Rakesh Gautam</dc:creator>
      <pubDate>Mon, 11 Nov 2019 04:09:19 +0000</pubDate>
      <link>https://dev.to/rkshrksh/using-defaultdict-in-python-2on3</link>
      <guid>https://dev.to/rkshrksh/using-defaultdict-in-python-2on3</guid>
      <description>&lt;p&gt;&lt;em&gt;Cross posted from &lt;a href="https://rakeshgautam.com/posts/using-defaultdict-in-python/"&gt;Rakesh's Blog&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Dictionaries are a great convenient way to store and retrieve data by name in python.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;mydict&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;'first_name'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'Rakesh'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'last_name'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'Gautam'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mydict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'first_name'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="n"&gt;Rakesh&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mydict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'last_name'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="n"&gt;Gautam&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But if you try to get an item with a key that is not currently in the dictionary, Regular Python dictionary raises a &lt;code&gt;KeyError&lt;/code&gt; exception.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;mydict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'middle_name'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="n"&gt;Traceback&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;most&lt;/span&gt; &lt;span class="n"&gt;recent&lt;/span&gt; &lt;span class="n"&gt;call&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
  &lt;span class="n"&gt;File&lt;/span&gt; &lt;span class="s"&gt;"&amp;lt;stdin&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;line&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;module&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nb"&gt;KeyError&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'middle_name'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While this is easy to handle manually, the &lt;code&gt;defaultdict&lt;/code&gt; simplifies this kind of task.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;DefaultDict&lt;/code&gt; works exactly like a regular python &lt;code&gt;Dict&lt;/code&gt;, but it is initialized with a function, that takes no argument and provides a default value for a non-existent key.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;DefaultDict&lt;/code&gt; never raises a &lt;code&gt;KeyError&lt;/code&gt; exception. If any key does not exist, its value gets returned by the DefaultDict.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;DefaultDict&lt;/code&gt; is part of the python's &lt;code&gt;collections&lt;/code&gt; package.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;collections&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;defaultdict&lt;/span&gt;

&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;mydict&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;'first_name'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'Rakesh'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'last_name'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s"&gt;'Gautam'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;def_dict&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;defaultdict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;mydict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;def_dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'first_name'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="n"&gt;Rakesh&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;def_dict&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'middle_name'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;

&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, this didn't raise any &lt;code&gt;KeyError&lt;/code&gt; exception because we passed &lt;code&gt;str&lt;/code&gt; as an argument to the &lt;code&gt;defaultdict&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;def_dict&lt;/code&gt; returns &lt;code&gt;""&lt;/code&gt; (default value for string) here if we pass a non-existant key as an argument.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;my_dict&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="s"&gt;'a'&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'b'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'c'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;dict_2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;defaultdict&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;my_dict&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="o"&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dict_2&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'x'&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="mi"&gt;0&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we passed &lt;code&gt;lambda : 0&lt;/code&gt; as a function to defaultdict, and this will return &lt;code&gt;0&lt;/code&gt;, whenever we ask for a non-existent key.&lt;/p&gt;

&lt;p&gt;We can use any function that takes no argument, in the &lt;code&gt;defaultdict&lt;/code&gt;. If someone attempts to access a key to which no value has been assigned, that function will be called (without arguments) and its return value is used as the default value for the key.&lt;/p&gt;

</description>
      <category>python</category>
      <category>defaultdict</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Namedtuple in Python</title>
      <dc:creator>Rakesh Gautam</dc:creator>
      <pubDate>Sun, 20 Oct 2019 05:50:10 +0000</pubDate>
      <link>https://dev.to/rkshrksh/namedtuple-in-python-43mi</link>
      <guid>https://dev.to/rkshrksh/namedtuple-in-python-43mi</guid>
      <description>&lt;p&gt;&lt;em&gt;Cross posted from &lt;a href="http://rakeshgautam.com/posts/namedtuple-in-python/"&gt;Rakesh's Blog&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;To understand NamedTuple, you first have to understand what a regular Tuple in Python is.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is a Tuple
&lt;/h3&gt;

&lt;p&gt;A Tuple in Python is similar to a List. The difference between a Tuple and a List is that the Tuples are Immutable, which means that you can't change the elements of a Tuple once it is assigned.&lt;/p&gt;

&lt;p&gt;The other difference is that the Tuples are defined using parentheses &lt;code&gt;()&lt;/code&gt; instead of square brackets &lt;code&gt;[]&lt;/code&gt; in a list.&lt;/p&gt;

&lt;p&gt;Tuples are also indexed with Integers, like List. To access a value in a tuple, you have to provide its numeric index.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Namedtuple
&lt;/h3&gt;

&lt;p&gt;As the name suggests, Namedtuples are just Tuples but with Names assigned to its elements.  NamedTuples can do everything that a Tuple can do. But the advantage of NamedTuples over Tuple is that you can access its values using the numeric index as well as the Names preassigned to its fields.&lt;/p&gt;

&lt;p&gt;When the number of elements is large, it becomes hard to remember which index is what. So namedtuple comes handy in that situation with all the features of a Normal Tuple and an added feature of accessibility with field names.&lt;/p&gt;

&lt;p&gt;This problem can also be solved by creating a class with read-only properties and accessing values by &lt;code&gt;classname.fieldname&lt;/code&gt;. But a class seems Overkill for this purpose. With Namedtuples, we avoid all the code for a class and still can access the values like &lt;code&gt;record.fieldname&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You can still use the index number to access the values stored. So namedtuple is like a compromise between a Tuple and a List/Dictionary in Python.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to create a Namedtuple
&lt;/h3&gt;

&lt;p&gt;Namedtuple is present in the &lt;code&gt;collections&lt;/code&gt; module of Python.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; from collections import namedtuple
&amp;gt;&amp;gt;&amp;gt; Person = namedtuple("Person", "name age sex")
&amp;gt;&amp;gt;&amp;gt; person1 = Person("Rakesh", 24, "male")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Each namedtuple is represented by its own class, created by using the &lt;code&gt;namedtuple()&lt;/code&gt; factory method. The first argument is the name of the new class/namedtuple and the second argument is a string containing the names of the elements, separated by spaces or commas.&lt;/p&gt;

&lt;p&gt;After creating the structure and providing values to the namedtuple as shown in the above code block, you can use the dot notation to access the values in our Person namedtuple.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; print(person1.name)
Rakesh
&amp;gt;&amp;gt;&amp;gt; print(person1.age)
24
&amp;gt;&amp;gt;&amp;gt; print(person1.sex)
male
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The values can also be accessed using the numeric index like the regular tuples and lists.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; person1[0]
'Rakesh'
&amp;gt;&amp;gt;&amp;gt; person1[1]
24
&amp;gt;&amp;gt;&amp;gt; person1[2]
'male'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Both above code blocks provide the same output, but with different access methods. So you can use whichever notation you find useful.&lt;/p&gt;

&lt;h3&gt;
  
  
  Attributes of a Namedtuple
&lt;/h3&gt;

&lt;h4&gt;
  
  
  _fields
&lt;/h4&gt;

&lt;p&gt;Use this to get the field names of all the items in a namedtuple.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; person1._fields
('name', 'age', 'sex')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
  
  
  _replace
&lt;/h4&gt;

&lt;p&gt;Namedtuple is immutable but we can use this &lt;code&gt;_replace&lt;/code&gt; which returns a shallow copy of the namedtuple, with specified property values changed. The original namedtuple stays unchanged.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; person1._replace(name="Gautam")
Person(name='Gautam', age=24, sex='male')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
  
  
  _asdict
&lt;/h4&gt;

&lt;p&gt;This returns the contents of a namedtuple as an ordered dictionary.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; person1._asdict()
OrderedDict([('name', 'Rakesh'), ('age', 24), ('sex', 'male')])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The above code returns person1 namedtuple as an OrderDict, in the order, they were added.&lt;/p&gt;

&lt;h4&gt;
  
  
  _make
&lt;/h4&gt;

&lt;p&gt;This can be used to create an instance of a namedtuple from a sequence or iterable. It comes handy when you have to create a Namedtuple from Existing data.&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;gt;&amp;gt;&amp;gt; p2 = ["Eli", 21, "male"]&lt;br&gt;
&amp;gt;&amp;gt;&amp;gt; person2 = Person._make(p2)&lt;br&gt;
&amp;gt;&amp;gt;&amp;gt; person2&lt;br&gt;
Person(name='Eli', age=21, sex='male')&lt;br&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Benefits of using Namedtuples&lt;br&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;You can access values using the names of the fields as well as the indexes.&lt;/li&gt;
&lt;li&gt;Namedtuples are as memory efficient as regular Tuples because it does not have per-instance dictionaries. This is also why it is faster than a dictionary.&lt;/li&gt;
&lt;li&gt;Improves the readability of your code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, hopefully, this kind of clears what a Namedtuple is and how it can be used to improve the readability and to write clean code.&lt;/p&gt;

</description>
      <category>python</category>
      <category>namedtuple</category>
      <category>tutorial</category>
      <category>example</category>
    </item>
  </channel>
</rss>
