Given a list such as a list of integers. How to print the list elements in one line?
Input: [1, 2, 3, 4, 5] Output: 1 2 3 4 5
Method 1: Print One Line List With Asterisk
To print all list of elements in one line, unpack the list into the built-in
print() function. In particular, you use the asterisk operator as a prefix in front of the list to unpack all elements into the argument list of the
print() function. For example, to print the list
[1, 2, 3] in one line, use
print(*[1, 2, 3]).
Here’s an example on an integer list:
>>> print(*[1, 2, 3, 4, 5]) 1 2 3 4 5
Here’s another example with mixed data types:
>>> print(*['alice', 'bob', 'carl', 42]) alice bob carl 42
You can watch my explainer video on the asterisk operator here:
Method 2: Print One Line List Separator
Say, you want to print a list of elements in one line using a specific separator between the elements.
To accomplish this, unpack the list into the built-in
print() function using the asterisk operator as a prefix in front of the list. This unpacks all elements into the argument list of the
print() function. Now, add the desired separator argument, e.g.,
sep='\t' to use the tabular character as a separator. For example, to print the list
[1, 2, 3] in one line, separatred by tab chars, use
print(*[1, 2, 3], sep='\t').
Here’s an example on an integer list:
>>> print(*[1, 2, 3, 4, 5], sep='\t') 1 2 3 4 5
To learn more about the separator argument, check out my detailed tutorial here:
Method 3: Simple For Loop with the Print Function’s End Argument
To print a list and display it in a single line, a straightforward solution is to iterate over each element in a for loop and print this element into the same line using the
print() function with the
end=' ' argument set to the empty space. This prevents the
print() function to automatically print each element to a new line.
Here’s how this is done for a mixed-type list:
>>> lst = [1, 2, 3, 4, 5, 'alice', 'bob'] >>> for x in lst: print(x, end= ' ') 1 2 3 4 5 alice bob
You can also use another
end argument such as the tabular character
'\t' like so:
>>> lst = [1, 2, 3, 4, 5, 'alice', 'bob'] >>> for x in lst: print(x, end= '\t') 1 2 3 4 5 alice bob
Method 4: str.join() on List of Strings
' '.join(list) method concatenates all elements in a given list of strings using the empty space (on which the method is called) as a separator argument.
Here’s how this works out to print a list of strings in a single line by first creating a one-line string using the join method and then printing that string to the shell:
>>> ' '.join(['the', 'answer', 'is', '42']) 'the answer is 42'
You can master the
join() method and other string methods here:
Method 5: str.join() and List Comprehension on General List
' '.join(list) method concatenates all elements in a given list of strings using the empty space (on which the method is called) as a separator argument. To convert each element in the list to a string value first, use the list comprehension statement
[str(x) for x in list]. You can combine both to
' '.join([str(x) for x in list]) to print all elements in a general list to the standard output in one line.
Here’s how this works:
>>> ' '.join(str(x) for x in lst) '42 python 3.3 (1, 2)'
You can master generator expressions here and list comprehension here:
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.