5 Useful Tricks in Python

Sneha Bhat
Nov 3 · 3 min read

These are some of the tricks I use to make my life easier.

Python 3
  1. map( )
mapping

This is one of my favorite functions. It is quite resourceful as what usually takes several lines to achieve can now be done in just a few lines ,thanks to this amazing built-in function

Converting string of integers to list of integers

This trick is especially handy while accepting user input and in competitive coding competitions

n=list(map(int,input().split()))

Here a space separated string of integers which was given as user input is now converted to a list of integers

(Note: Notice how many functions are combines in a single line? The beauty of python apart from many built-in functions is the freedom to combine and use many functions in a single line, just like the above example)

2. zip( )

This function is useful when there are 2 or more iterable containers that you need to combine. Something like 2 lists containing related data, say, one list contains names of students and the other contains their respective roll numbers. zip() combines these containers index-wise into a ‘string’ of tuples.

a=[1,2,3]

b=[3,4,5]

k=zip(a,b)

print(list(k))

output: [(1,3),(2,4),(3,5)]

3. enumerate( )

enumerate()

This function is similar to zip( ), as it combines 2 entities into one. But the catch here is that it takes 2 parameters. one is the iterator other is a value from where the index starts. if index is not mentioned it takes default value as 0.

k=’python’

m=enumerate(k)

print(list(m))

output: [(0,’p’),(1,’y’),(2,’t’),(3,’h’),(4,’o’),(5,’n’)]

So next time you need to index something, remember you can use this

4. filter( )

filter( )

This is yet another lovely built in function in python. This function filters a given sequence based on a given function or condition , and returns a sequence of entities in the original sequence that satisfy this condition. So instead of writing a loop to iterate over a sequence to check if they satisfy a condition, it can now be done in a single line.

j=[1,2,3,4,5,6,7,8]

k=filter(lambda x : x%2 == 0, j)

print(k)

output :[2,4,6,8]

5. List Comprehension

This is trick that combines an entire loop into a single line. Suppose we need to create a list of squared integers from a list of integers. Conventional way would be to loop over the integers and square them individually and then append them to a list or you could save yourself the trouble of writing more lines of code and combine this into a single line instead.

m=[1,2,3,4]

k=[x**2 for i in m]

print(k)

output: [1,4,9,16]

Well, there you have it. So these are the tricks I use while using Python. What about you? Do let me know.

Sneha Bhat

Written by

A Data Science enthusiast, who loves anime, music and animals

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade