Python “tricks” I can not live without

Sebastian Opałczyński
Level Up Coding
Published in
7 min readJan 12, 2021

--

It’s python.

There is plenty of similar articles on the web, but I just thought that I can add my perspective on the topic, and make the “tricks” list more complete. The code fragments used here are somehow crucial to my workflow and I am reusing them over and over again.

Sets

It’s often the case that developers tend to forget that python has set datatype and trying to use lists for everything. What is set? Well, long story short:

A set is an unordered collection with no duplicate elements.

If you get familiar with sets and their logic it can solve a lot of problems for you. For example — how to get all of the unique letters used in a word?

myword = "NanananaBatman"
set(myword)
{'N', 'm', 'n', 'B', 'a', 't'}

Boom. Problem solved, but to be honest it’s from the official python documentation, so nothing surprising.

What about this? Get a list of items without element repetition?

# first you can easily change set to list and other way aroundmylist = ["a", "b", "c", "c"]# let's make a set out of itmyset = set(mylist)# myset will be:
{'a', 'b', 'c'}

--

--

Software engineer, technology enthusiast, common sense lover, serial co-founder, and a father of three.