Hackernoon logoPython For Beginners: Learning One-Liners On Practice by@riade

Python For Beginners: Learning One-Liners On Practice

Riade Hacker Noon profile picture


My name is Riade, I am a full stack web developer, python and artificial intelligence programmer.

Like lists comprehensions and lambda functions python one line codes can
save a lot of time and space so how you can master them?


Probably you have seen one line code that can replace entire 3 lines code in python for example a for loop, maybe this is why python is so popular and easy to use but what is this for loop one line code that can replace a 3 lines code?

Let’s take an example:

some_list = []
for i in range(10):

Just easy and simple for loop in python that append numbers in list, so what can you do to write this in one line:

some_list = [i for i in range(10)]

Now look at that! This is a fully functional code that can store numbers
0–9 in this list and this is exactly the same as the previous code.

So in this article I will try to explain as much 1 line codes as I can. Let’s start with the previous code, this is called Lists Comprehensions And you can do nested loops as well, a condition inside the loops and basically what is possible with normal loop is possible in Lists Comprehensions, let’s see a few examples:

# Simple List Comprehension
  some_list = [i for i in range(10)] 
# Nested Loops
  some_list = [[i for i in range(10)] for j in range(10)]
  some_list = [i for i in range(10) for j in range(10)]
# List Comprehension & Conditions
  some_list = [ x for x in range(20) if x % 2 == 0]
  some_list = [ y for y in range(20) if y != 2] 

I guess you get the idea, you can be creative as much as you can with Lists Comprehensions, they are easy and fun to use and can save you time if you know what are you doing.

For our next example we will check if a certain string is palindrome, a standard approach will be to take your string, convert it to a list and then reverse that list, join the list letters and compare if this word is the same as your input:

T = "anna"
lst = []
for i in T:
print("".join(lst) == T)

Long process just to get if a word is the same, how we can optimize this using only one line of code:

T = "anna"
print(T[::-1] == T)

Yeah, that’s it, this code will reverse your input and compare it with the
original text if it’s the same the find function will return True else False. Of course, you can do this in more than a way you just need to be creative!

By now you can see a pattern here, One line codes in python are more easy, clear that a standard detailed code that can save time and space(I will write an article about space complexity soon). Ok let us take 2 more

Let’s swap 2 variables, of course I will start with the standard approach, you have 2 variables and you want to swap value you need to assign one
of the values to a new variable then move the value from one to other and then reassign the moved value in the begging to the last variable it may look like this:

a = 10
b = 20
temp = a
a = b
b = temp

A lot of work just to swap 2 variables, let’s swap this with one line of code:

a = 10
b = 20
a, b = b, a

That’s all you need to do, this is a lot simpler than writing all those lines just to swap 2 variables. Now suppose that we have a list and you want to sum all the numbers from index 5 to the last index, normally youhave to use loops in order to achieve this kind of behavior but what about this:

print(sum([1, 2, 3, 4, 5, 6, 7][5:]))

And again without any loops this was all you need to do a one line syntax that can replace mini-code and can save you a lot of space.


This was an introduction to how you can write One Line Codes in python,
there are more to explore out there this is just to give you an idea if you are curious to learn more all you need is to practice, develop your creativity because it is the key to learn.

Start by leveling up your knowledge in python to help you think better about other syntaxes, this is not a full list there is a lot and lot to learn and be sure this will save you if you are a data science or data analysis.

If you have any question leave a comment or contact at [email protected] and I will try to reply as soon as possible and until then keep learning.

Also published at https://riade.medium.com/data-science-fundamentals-how-to-master-python-one-line-codes-4f0a23cd3977

Riade Hacker Noon profile picture
by Riade @riade. My name is Riade, I am a full stack web developer, python and artificial intelligence programmer.Read my stories


Join Hacker Noon

Create your free account to unlock your custom reading experience.