Leveraging print() to fullest
If we need to print the values of a list in a single line separated by spaces, we can do it simply by passing an iterator to the list in print() as a parameter.
Traditional way
Python3
nums = [ 1 , 2 , 3 ] for i in nums: print (i, end = " " ) |
One Liner
Python3
nums = [ 1 , 2 , 3 ] print ( * nums) # Output 1 2 3 |
This is because the definition of the print interface in Python documentation itself specifies that if we pass an iterator to an object in the print function, the values will be printed separated by a space and will end with a newline
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
10 Useful Python One Liners That Developers Must Know
Python is known for its easy-to-code and intuitive syntax. And one-liners are like the cherry on the cake which makes Python a more beautiful and beloved programming language. There are numerous features in Python Language and one of its most loved features is its one-liners. Writing Code in Python is already easy compared to other programming languages and using the one-liners makes it more easier and cool. Now let’s see what are these Python one-liners.
Contact Us