# How to Print a List in Python

To print the contents of a list in Python, all you need to do is call the `print()` function on it.

Here is an example of printing a list of three fruits in a list:

```fruits = ["Banana", "Apple", "Orange"]

print(fruits)```

Output:

`['Banana', 'Apple', 'Orange']`

But if you want to print each element of a list separately, you should use a for loop.

For instance, let’s print the three elements of a list on separate lines by using a for loop:

```fruits = ["Banana", "Apple", "Orange"]

for fruit in fruits:
print(fruit)```

Output:

```Banana
Apple
Orange```

These answers are probably enough, but in case you’re interested, feel free to keep on reading to learn more approaches to printing lists in Python.

## Alternative Ways to Print a List in Python

Besides passing the list to a print function or using a loop to print each element, there are three great alternative ways to print list items in Python.

1. Use the asterisk operator (`*`).
2. Use the `list.join()` method.
3. Use the `map()` function.

Let’s take a closer look at these approaches and why you might want to consider those.

### 1. Use an Asterisk to Print a List in Python

To print a list without commas or square brackets without using a loop, you can use the asterisk to unpack the list elements.

```fruits = ["Banana", "Apple", "Orange"]

print(*fruits)```

Output:

`Banana Apple Orange`

The asterisk operator in Python is known as the unpacking operator. You can use it to pull values from iterables, such as lists, strings, or tuples.

And by the way, if you want to have a separator between the printed list elements, specify `sep` parameter in the `print()` function call.

For instance, let’s print the list of elements using the unpacking operator and separate the elements by a comma and a blank space:

```fruits = ["Banana", "Apple", "Orange"]

print(*fruits, sep=", ")```

Output:

`Banana, Apple, Orange`

Notice that the separator can be anything.

For example, you can use this approach to print each element on a new line by:

```fruits = ["Banana", "Apple", "Orange"]

print(*fruits, sep="\n")```

Output:

```Banana
Apple
Orange```

### 2. Use the Built-In join() Method for Lists of Strings

If you have a list of strings, you can use the `string.join()` method to combine the strings and print the result.

For example:

```fruits = ["Banana", "Apple", "Orange"]

print(" ".join(fruits))```

Output:

`Banana Apple Orange`

Notice that the `.join()` method belongs to the string type. So this does not work for example when the list consists of integers.

### 3. Use the Built-In Map Function to Print a List in Python

To print a list of integers similar to how you printed the strings in the previous example:

1. Use the `map()` function to transform them into strings.
2. Call `join()` method to merge them into one string.
3. Print the resulting string out using the `print()` function.

For instance:

```nums = [1, 2, 3, 4, 5]
print(" ".join(map(str, nums)))```

Output:

`1 2 3 4 5`

Let’s take a quick look at how this code works.

1. The `map()` function creates a new list that contains each integer as a string.
2. Then the `join()` method combines all the strings in the new list into one string that is printed out.

## Conclusion

That’s a whole bunch of ways to print lists in Python.

Before you go, here is a quick recap of the things to take home.

The easiest way for you to print a list in Python is by just calling `print()` function on a list:

```fruits = ["Banana", "Apple", "Orange"]
print(fruits)```

To separately print each element, use a for loop:

```fruits = ["Banana", "Apple", "Orange"]

for fruit in fruits:
print(fruit)```

Alternatively, you can use

• String’s `join()` method.
• Asterisk unpacking (*).
• The `map() `function with the `join()` method.

Thanks for reading. I hope you find it useful.

Happy coding!
{"ft_lang_label":"__label__en","ft_lang_prob":0.77707356,"math_prob":0.82324505,"size":4162,"snap":"2023-14-2023-23","text_gpt3_token_len":1029,"char_repetition_ratio":0.14093314,"word_repetition_ratio":0.06789251,"special_character_ratio":0.25540605,"punctuation_ratio":0.13461539,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9677022,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-03-20T10:08:05Z\",\"WARC-Record-ID\":\"<urn:uuid:b03a6e35-1301-4268-b7c4-d4457479a43d>\",\"Content-Length\":\"171205\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:10842fb8-591e-4c32-b704-f223eb8b8f94>\",\"WARC-Concurrent-To\":\"<urn:uuid:8615ead8-c8de-451d-8078-312702a9e2bf>\",\"WARC-IP-Address\":\"46.30.213.110\",\"WARC-Target-URI\":\"https://www.codingem.com/print-a-list-in-python/\",\"WARC-Payload-Digest\":\"sha1:ZJWMXOYKTADY7D42PR6ITMGR2KV5DR7Z\",\"WARC-Block-Digest\":\"sha1:UAFVX7XYU4O677JWD7EDLB67PLQY6YIJ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-14/CC-MAIN-2023-14_segments_1679296943471.24_warc_CC-MAIN-20230320083513-20230320113513-00747.warc.gz\"}"}