How To Print A List In Python - Codingem
Maybe your like
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 OrangeThese 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.
- Use the asterisk operator (*).
- Use the list.join() method.
- 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 OrangeThe 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, OrangeNotice 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 Orange2. 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 OrangeNotice 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:
- Use the map() function to transform them into strings.
- Call join() method to merge them into one string.
- 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 5Let’s take a quick look at how this code works.
- The map() function creates a new list that contains each integer as a string.
- 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!
Further Reading
- 50 Python Interview Questions and Answers
Tag » How To Print A List Python
-
Print Lists In Python (5 Different Ways) - GeeksforGeeks
-
How To Print A List In Python? - FavTutor
-
3 Easy Methods To Print A Python List - AskPython
-
Python List Print - 7 Different Ways To Print A List You Must Know
-
How To Print A List In Python? | Flexiple Tutorials
-
How To "properly" Print A List? - Python - Stack Overflow
-
Python List Print - Initial Commit
-
Python List Index() - Programiz
-
Python Print List Example - HowToDoInJava
-
Python: Generate And Print A List Of First And Last 5 Elements Where ...
-
Create And Print Lists Using Python
-
Python Tips - How To Easily Convert A List To A String For Display
-
Python List Reverse() - Programiz
-
6 Ways To Print Lists Of List In Python