<?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: @QED</title>
    <description>The latest articles on DEV Community by @QED (@atqed).</description>
    <link>https://dev.to/atqed</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F496012%2F4b8bf566-2c56-4ed6-94d2-3d4139d766c5.jpg</url>
      <title>DEV Community: @QED</title>
      <link>https://dev.to/atqed</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/atqed"/>
    <language>en</language>
    <item>
      <title>You can be happy to know Python Counter - How to get the most common elements in a list</title>
      <dc:creator>@QED</dc:creator>
      <pubDate>Wed, 21 Oct 2020 18:01:53 +0000</pubDate>
      <link>https://dev.to/atqed/you-can-be-happy-to-know-python-counter-how-to-get-the-most-common-elements-in-a-list-o1m</link>
      <guid>https://dev.to/atqed/you-can-be-happy-to-know-python-counter-how-to-get-the-most-common-elements-in-a-list-o1m</guid>
      <description>&lt;p&gt;I often use Python Counter to count elements of a list. The Counter class is a dict subclass and can be used from Python 3.1. The following is the most simplest example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from collections import Counter

s = ['a', 'a', 'b']

c = Counter(s)

print(c)  # Counter({'a': 2, 'b': 1})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can count the specified element of a list in the same way as getting the value of a dictionary by a key.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from collections import Counter

s = ['a', 'a', 'b']

c = Counter(s)

print(c['a'])  # 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You don't need to create own functions to count items like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;s = ['a', 'a', 'a', 'b', 'b', 'c']

c = 0

for i in s:
    if i == 'a':
        c = c + 1

print(c)  # 3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Get the most common elements
&lt;/h2&gt;

&lt;p&gt;The most_common() is one of the greatest method in the Counter class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from collections import Counter

s = ['a', 'a', 'a', 'a', 'b', 'b', 'b', 'c']

c = Counter(s)

common = c.most_common(2)

print(common)  # [('a', 4), ('b', 3)]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function returns the n most common elements as a list and n is 2 in the above case. If n is omitted, it returns all the elements and counts.&lt;/p&gt;

&lt;p&gt;The detailed explanation is &lt;a href="https://www.atqed.com/python-counter"&gt;here&lt;/a&gt;. The collections module, which has the Counter class, helps us to shortcut developing. If you want to create the tiny or primitive function that might need the for statement, it's important to rethink if it's been already coded by Python Software Foundation.&lt;/p&gt;

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