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 , maybe this is why python is so popular and easy to use but what is this that can replace a 3 lines code? for loop for loop one line code Let’s take an example: some_list = [] i range( ): some_list.append(i) for in 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 i range( )] for in 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 And you can do nested loops as well, a condition inside the loops and basically what is possible with normal loop is possible in let’s see a few examples: Lists Comprehensions Lists Comprehensions, some_list = [i i range( )] some_list = [[i i range( )] j range( )] some_list = [i i range( ) j range( )] some_list = [ x x range( ) x % == ] some_list = [ y y range( ) y != ] # Simple List Comprehension for in 10 # Nested Loops for in 10 for in 10 for in 10 for in 10 # List Comprehension & Conditions for in 20 if 2 0 for in 20 if 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 = lst = [] i T: lst.append(i) print( .join(lst) == T) "anna" for in "" Long process just to get if a word is the same, how we can optimize this using only one line of code: T = print(T[:: ] == T) "anna" -1 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 examples: 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 = b = temp = a a = b b = temp 10 20 A lot of work just to swap 2 variables, let’s swap this with one line of code: a = b = a, b = b, a 10 20 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. Conclusion: 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 and I will try to reply as soon as possible and until then keep learning. riadeboughaba@gmal.com Also published at https://riade.medium.com/data-science-fundamentals-how-to-master-python-one-line-codes-4f0a23cd3977