• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Python Print List – Printing Elements of List to the Console

Python Print List – Printing Elements of List to the Console

February 11, 2022 Leave a Comment

In Python, we can print a list easily. We can use loops, the Python join() function, or the Python * operator to print all elements of a list.

The first example shows how to print a list using a loop.

list = ["This","is","a","list","of","strings"]

for x in list:
    print(x)

#Output:
This
is
a
list
of
strings

You can also use the Python join() function to print the items of a list.

list = ["This","is","a","list","of","strings"]

print(" ".join(list))

#Output:
This is a list of strings

Another method is you can use the Python * operator to print a list in Python.

list = ["This","is","a","list","of","strings"]

print(*list)

#Output:
This is a list of strings

Finally, if you have elements which aren’t strings in your list, you can use the map() function.

list = ["This","is","a","list","of","strings",1,2,3,4,5]

print(" ".join(map(str, list))) 

#Output:
This is a list of strings 1 2 3 4 5

When working with collections, it is useful to be able to print the items of a list or dictionary.

We can easily print the elements of a list in Python using a number of different methods.

We can print a list by using the Python print() function. This will print the list object to the console.

list = ["This","is","a","list","of","strings"]

print(list)

#Output:
["This","is","a","list","of","strings"]

Typically, we want to get only the elements of the list, and not the list. To print the items of a list to the console with Python, we can use a for loop to print the elements of a list.

list = ["This","is","a","list","of","strings"]

for x in list:
    print(x)

#Output:
This
is
a
list
of
strings

Using the Python join() Function to Print List in Python

Another method for printing a list in Python is to use the Python join() function. You can use the join() function on a delimiter string, and pass a list as an argument to join the elements of the list by the delimiter.

To print a list with the join() function, we first join the elements of the list by spaces, and then pass it to print().

list = ["This","is","a","list","of","strings"]

print(" ".join(list))

#Output:
This is a list of strings

Using the Python * Operator to Print List

Another method that we can print a list in Python is to use the Python * operator. The * operator unpacks each element of a list.

We can use the Python * operator to print a list as shown in the following Python code.

list = ["This","is","a","list","of","strings"]

print(*list)

#Output:
This is a list of strings

If you want to print the elements of a list on different lines, you can pass ‘\n’ to the optional “sep” argument.

list = ["This","is","a","list","of","strings"]

print(*list, sep='\n')

#Output:
This
is
a
list
of
strings

Using the Python map() Function to Print List in Python

Finally, we use the Python map() function in combination with the Python join() function to print the elements of a list.

The map() function is useful if your string has items which are not strings because it will convert the elements to string variables before joining them.

Below is an example in Python of how to print a list using the map() function.

list = ["This","is","a","list","of","strings",1,2,3,4,5]

print(" ".join(map(str, list))) 

#Output:
This is a list of strings 1 2 3 4 5

Printing the First N Elements of a List in Python

In Python, we can easily print the first n items in a list. To do so, we can use a loop as shown in the first example of this article.

To print the first n items of a list, we just loop over those first n elements and print them out.

Below is an example which prints out the first 10 items of a list in Python.

list = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

for i in range(0,10):
    print(list[i])

#Output:
0
1
2
3
4
5
6
7
8
9

Hopefully this article has been useful for you to understand how to print lists using Python.

Other Articles You'll Also Like:

  • 1.  How to Rotate a List in Python
  • 2.  Python isprime – Function for Determining if Number is Prime
  • 3.  Format Numbers as Dollars in Python with format()
  • 4.  PROC LIFETEST Equivalent in Python
  • 5.  Get Current Datetime in Python
  • 6.  Remove Duplicates from Sorted Array in Python
  • 7.  Using Python to Insert Tab in String
  • 8.  Check if Character is Letter in Python
  • 9.  Python Destroy Object – How to Delete Objects with del Keyword
  • 10.  List of Turtle Shapes in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy