🀫 Secret data types smarter than lists, dicts


Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python.

​

We all know Python’s basic container data types β€” lists, dictionaries, tuples, sets. "Container" is just a data type that holds multiple items instead of single values.

​

However, many times basics are not enough. Like if you want count of different items in the list you will run a loop like below.

In:
fruits = ["apple", "banana", "apple", "cherry"]
counts = {}
for item in fruits:
    counts[item] = counts.get(item, 0) + 1
print(counts)
Out: {'apple': 2, 'banana': 1, 'cherry': 1}

​

Can this be done in 1 line? Sure, here comes the collections module. It has specialized containers built for these exact jobs.

Today, we explore Counter from collections β€” Count things right away.

​

πŸ› οΈ Import (load) "Counter" from collections module

In:
from collections import Counter
Out:

​

✨ Count items of each type in a flash

In:
fruits = ["apple", "banana", "apple", "cherry"]
count = Counter(fruits)
print(count)
Out: Counter({'apple': 2, 'banana': 1, 'cherry': 1})

πŸ’‘ looks like a dictionary but its not! Check 'type'

​

In:
print(type(count))
Out:

​

πŸ“š Most common element

In:
letters = ["a", "b", "a", "c", "a", "b"]
print(Counter(letters).most_common(1))
Out: [('a', 3)]

​

πŸ’« Convert Counter results to dictionaries

In:
c = Counter("aabccc")
print(dict(c))
Out: {'a': 2, 'b': 1, 'c': 3}

​

πŸš€ Update counts on the go

In:
c = Counter()
c.update(["x", "y", "x"])
print(c)

c.update(["x"])
print(c)
Out:
Counter({'x': 2, 'y': 1})
Counter({'x': 3, 'y': 1})

​

🚫 Subtract counts easily

In:
c = Counter("aaabb")
print(c)

c.subtract("ab")
print(c)
Out:
Counter({'a': 3, 'b': 2})
Counter({'a': 2, 'b': 1})

β­πŸ“£ More coming on this tomorrow. That's it for today! If you liked it, please share it with anyone who will find it useful 🐼🐼

​

Pandas Daily

Beginner to Expert in Python in just 5 minutes

Read more from Pandas Daily

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. We all agree that dashboards, emails, logs should read like English. Meaning 1.2 million beats 1234567 and 3.1M is more readable than 3145728. Stakeholders scan - and they get pissed if they have to parse messy numbers 😭😭 So today, we explore a third-party humanize library. Format numbers, file sizes, and dates/times into human-readable text. Mini exercise at the end. πŸ₯‡ Ready, Get, Set, Go! Install and...

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Amazon reviews, chats and social media text (posts, comments, stories etc) are full of emojis πŸ”₯πŸ”₯ Great for expression! But give a hard time for text interpretation (Amazon, Meta would be going crazy). That is where Python's demoji library comes in. From a given text you can: Extract emojis Remove them if needed Replace with textual meaning Understand sentiment and what not... Let's begin!! Install demoji...

Welcome back to Pandas Daily! Your daily 5-minute boost to becoming confident in Python. Do you want to outsmart your friends (and interviewers) with Python? Couple of weeks back we played around with date and time using datetime module. Guess what - Python has another super handy module: calendar. Allows you to do all things related to calendars - like literally printing it in a readable form too. And its extremely simple to learn! Yet not many do it πŸ˜” Lets Go πŸš€πŸš€ ✨ Import calendar module...