<?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: Afiz</title>
    <description>The latest articles on DEV Community by Afiz (@afizs).</description>
    <link>https://dev.to/afizs</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%2F385678%2F8723558f-72f7-4e78-abe3-94e1d39d4c2e.png</url>
      <title>DEV Community: Afiz</title>
      <link>https://dev.to/afizs</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/afizs"/>
    <language>en</language>
    <item>
      <title>Find the factorial of given number in Python</title>
      <dc:creator>Afiz</dc:creator>
      <pubDate>Mon, 06 Jun 2022 16:43:17 +0000</pubDate>
      <link>https://dev.to/afizs/find-the-factorial-of-given-number-in-python-1hji</link>
      <guid>https://dev.to/afizs/find-the-factorial-of-given-number-in-python-1hji</guid>
      <description>&lt;p&gt;Method 1: Finding the factorial of a given number using for loop&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def factorial(number):
    output = 1
    if number &amp;lt; 2:
        return output
    else:
        for i in range(2, number+1):
            output *= i
    return output

print(factorial(5))

# output: 120

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

&lt;/div&gt;



&lt;p&gt;Method 2: Finding the factorial of a given number using recursion&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def factorial_recursion(number):
    if number &amp;lt; 2:
        return 1
    return number * factorial_recursion(number -1)def factorial_recursion(number):
    if number &amp;lt; 2:
        return 1
    return number * factorial_recursion(number -1)

print(factorial_recursion(5))

# output: 120
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Method 3: Finding the factorial of a given number using the lambda function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;factorial_lambda = lambda number: 1 if number  &amp;lt; 2 else  number * factorial_lambda(number -1)

print(factorial_recursion(5))

# output: 120
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Thanks for reading this post. Let me know which one is your favorite method. &lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>interview</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to remove duplicates from a list in Python?</title>
      <dc:creator>Afiz</dc:creator>
      <pubDate>Thu, 13 Jan 2022 14:12:12 +0000</pubDate>
      <link>https://dev.to/afizs/how-to-remove-duplicates-from-a-list-in-python-2m6m</link>
      <guid>https://dev.to/afizs/how-to-remove-duplicates-from-a-list-in-python-2m6m</guid>
      <description>&lt;p&gt;In this post, we will discuss how to remove duplicates from a list in Python. &lt;/p&gt;

&lt;p&gt;There are many ways to delete duplicates from a list, but I find these two methods easy and readable. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Using &lt;code&gt;sets&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Using &lt;code&gt;fromkeys&lt;/code&gt; method of &lt;code&gt;dict&lt;/code&gt; &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Please take a look the examples below. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--I3Bhn1is--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bsslog0nwmb82m1ftlgq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--I3Bhn1is--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bsslog0nwmb82m1ftlgq.png" alt="Image description" width="880" height="633"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you like this content, consider following me. Thanks&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>python</category>
      <category>programming</category>
      <category>datascience</category>
    </item>
    <item>
      <title>How to find nth largest/smallest item from a List in Python</title>
      <dc:creator>Afiz</dc:creator>
      <pubDate>Tue, 04 Jan 2022 10:36:21 +0000</pubDate>
      <link>https://dev.to/afizs/how-to-find-nth-largestsmallest-item-from-a-list-in-python-32d0</link>
      <guid>https://dev.to/afizs/how-to-find-nth-largestsmallest-item-from-a-list-in-python-32d0</guid>
      <description>&lt;p&gt;Do you know you can find n largest/smallest items from a list using heapq python standard library?&lt;/p&gt;

&lt;p&gt;Check out this example below: &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--cMAljlBc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tlr3e7u4aa5pq0vtwzgo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--cMAljlBc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tlr3e7u4aa5pq0vtwzgo.png" alt="Image description" width="880" height="681"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you like this content consider following me. &lt;/p&gt;

</description>
      <category>python</category>
      <category>datascience</category>
      <category>machinelearning</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to sort a list in Python</title>
      <dc:creator>Afiz</dc:creator>
      <pubDate>Mon, 03 Jan 2022 17:26:40 +0000</pubDate>
      <link>https://dev.to/afizs/how-to-sort-a-list-in-python-2ok9</link>
      <guid>https://dev.to/afizs/how-to-sort-a-list-in-python-2ok9</guid>
      <description>&lt;p&gt;In this post we will discuss how to sort list in Python. &lt;/p&gt;

&lt;p&gt;There are mainly two ways to sort a list in Python.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;sorted&lt;/code&gt; function&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sort&lt;/code&gt; method &lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  1. &lt;code&gt;sorted&lt;/code&gt; function:
&lt;/h2&gt;

&lt;p&gt;This sort the given list and generate a new list, which means existing list won't be modified.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;In [1]: numbers = [23, 42, 12, 121, 45, 9, 8]

In [2]: sorted(numbers)
Out[2]: [8, 9, 12, 23, 42, 45, 121]

In [3]: numbers
Out[3]: [23, 42, 12, 121, 45, 9, 8]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see in the above example &lt;code&gt;Out[3]&lt;/code&gt; has the same numbers list. &lt;/p&gt;

&lt;h2&gt;
  
  
  2. &lt;code&gt;sort&lt;/code&gt; method:
&lt;/h2&gt;

&lt;p&gt;This method applies the sorting on the list and updates the existing list. This is called in place sorting.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;In [4]: numbers.sort()

In [5]: numbers
Out[5]: [8, 9, 12, 23, 42, 45, 121]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Key param:
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;sorted&lt;/code&gt; and &lt;code&gt;sort&lt;/code&gt; methods takes an extra parameter called &lt;code&gt;key&lt;/code&gt;. Using this &lt;code&gt;key&lt;/code&gt; param we can control the sorting. &lt;/p&gt;

&lt;p&gt;Example: &lt;code&gt;names = ['Bob', 'Charles', 'James']&lt;/code&gt;. Let's say we want to sort the above list of &lt;code&gt;names&lt;/code&gt; based on length of the each time. &lt;br&gt;
Solution:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;In [7]: sorted(names, key=len)
Out[7]: ['Bob', 'James', 'Charles']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can pass any function we want to this &lt;code&gt;key&lt;/code&gt;. Let me know which method you like in the comments. If you like this content consider following me. &lt;/p&gt;

</description>
      <category>python</category>
      <category>datascience</category>
      <category>machinelearning</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to create dictionary with two lists in Python</title>
      <dc:creator>Afiz</dc:creator>
      <pubDate>Sun, 02 Jan 2022 17:19:44 +0000</pubDate>
      <link>https://dev.to/afizs/how-to-create-dictionary-with-two-lists-in-python-5cla</link>
      <guid>https://dev.to/afizs/how-to-create-dictionary-with-two-lists-in-python-5cla</guid>
      <description>&lt;p&gt;In this post, we will discuss how to create a new dictionary out of two lists. &lt;br&gt;
First let's see the input and expected output and then the actual source code of the problem.&lt;/p&gt;

&lt;p&gt;input:&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="n"&gt;keys&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'name'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'age'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'contact'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;values&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'Afiz'&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="s"&gt;'9090909090'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;expected output:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{'name': 'Afiz', 'age': 30, 'contact': '9090909090'}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are multiple ways to solve this problem. &lt;/p&gt;

&lt;p&gt;Method 1: First let's see the simple way using &lt;code&gt;for&lt;/code&gt; loop in Python.&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 = {}

for i in range(len(keys)):
    my_dictionary[keys[i]] = values[i]

print(my_dictionary)

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

&lt;/div&gt;



&lt;p&gt;This solution is okay but not great. Let's check out another method which more pythonic way of doing it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;print(dict(zip(keys, values)))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Surprised !! 😮 😯 😲  yes it is one line. Tell me which method you like in the comment section. And finally if you want the explanation of these solutions please checkout my YouTube Channel: &lt;a href="https://youtu.be/PFsP2U4_GH0"&gt;https://youtu.be/PFsP2U4_GH0&lt;/a&gt; &lt;/p&gt;

</description>
      <category>python</category>
      <category>beginners</category>
      <category>datascience</category>
    </item>
    <item>
      <title>How to reverse a list in python? </title>
      <dc:creator>Afiz</dc:creator>
      <pubDate>Sun, 02 Jan 2022 17:11:12 +0000</pubDate>
      <link>https://dev.to/afizs/how-to-reverse-a-list-in-python-398d</link>
      <guid>https://dev.to/afizs/how-to-reverse-a-list-in-python-398d</guid>
      <description>&lt;p&gt;How do you reverse a list in Python? &lt;/p&gt;

&lt;p&gt;There are 2 good ways to reverse a list. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Slicing &lt;/li&gt;
&lt;li&gt;reverse method&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  1. Slicing: Generates new list and original list won't be updated.
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;In [1]: chars = list('abdbbef')

In [2]: chars[::-1]
Out[2]: ['f', 'e', 'b', 'b', 'd', 'b', 'a']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Reverse method: &lt;code&gt;reverse&lt;/code&gt; method modifies the original list.
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;In [3]: chars.reverse()

In [4]: chars
Out[4]: ['f', 'e', 'b', 'b', 'd', 'b', 'a']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Which method do you prefer? Share your views in the comments. &lt;/p&gt;

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