# Python Print One Line List

## Problem Formulation

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

The `' '.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

The `' '.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!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. 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?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming 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.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!