How do you print a list element in Python?

In this tutorial, we will look at how to print elements of a list in Python with the help of some examples.

How to Print List Elements?

How do you print a list element in Python?
How do you print a list element in Python?

There are multiple ways to print elements of a list in Python. For example, you can use a loop to iterate through and print the elements, you can use the

1
2
3
4
5
4 operator to unpack the elements in the list and directly print them, you can use the string
1
2
3
4
5
5 function to print the list elements as a single string, etc.

Let’s look at these methods with the help of examples.

Using Loop to Print List Elements

This is a straightforward approach. Iterate through and print the list elements one by one using a loop. Let’s look at an example.

# create a list
ls = [1,2,3,4,5]
# iterate over list elements and print them
for item in ls:
    print(item)

Output:

1
2
3
4
5

Here we use a for loop to iterate over the list

1
2
3
4
5
6 and print each element. Since we are explicitly iterating over each element, we can use this method for more complex list printing tasks. For example, using a specific format for each element, or only printing elements that satisfy a conditional statement.

Let’s print only the odd elements in a list of numbers.

# create a list
ls = [1,2,3,4,5]
# iterate over list elements
for item in ls:
    # print odd elements
    if item % 2 != 0:
        print(item)

Output:

1
3
5

Here we use a condition to check whether the current list element is odd or not, the element is only printed if it is odd.

Let’s look at another example – Print elements in a list of real numbers with only two digits after the decimal.

# create a list
ls = [1.456, 2.111, 3.605]
# iterate over list elements
for item in ls:
    # print with formatting
    print("{:.2f}".format(item))

Output:

1.46
2.11
3.60

Here, we format the element being printed such that it prints the element with only two digits after the decimal using a format string. You can read more about format strings .

Using 1 2 3 4 54 operator to unpack the list

You can also use the

1
2
3
4
5
4 operator to print the list elements. The
1
2
3
4
5
4 operator, when used before an iterable (for example, list, tuple, etc.) unpacks the elements of the iterable.

# create a list
ls = [1,2,3,4,5]
# use * to unpack list items
print(*ls)

Output:

1 2 3 4 5

The list elements are printed above. You can also specify the separator you want to use when printing the elements. Pass the separator you want to the

# create a list
ls = [1,2,3,4,5]
# iterate over list elements
for item in ls:
    # print odd elements
    if item % 2 != 0:
        print(item)
0 parameter of the
# create a list
ls = [1,2,3,4,5]
# iterate over list elements
for item in ls:
    # print odd elements
    if item % 2 != 0:
        print(item)
1 function. For example, let’s use a comma as a separator.

# create a list
ls = [1,2,3,4,5]
# use * to unpack list items
print(*ls, sep=",")

Output:

1,2,3,4,5

We get the elements separated by a comma.

Using string 1 2 3 4 55 function to print a list

The string join() function is commonly used to concatenate elements in a list of strings to a single string. You can also use it to print elements in a list provided you convert the elements to string type before the join operation.

1
2
3
4
5
0

Output:

1,2,3,4,5

Here we used a list comprehension to build a list of strings and then applied the string

1
2
3
4
5
5 function to print the list of elements separated by a comma as a single string.

Similar to the loop example, you can also make additional formatting changes. For example, print real numbers only up to two decimal places.

1
2
3
4
5
2

Output:

1
2
3
4
5
3

The numbers are printed with two decimal places. Using the format string converts the real numbers to string and thus there’s no need to convert it to a string again.


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • How do you print a list element in Python?
    How do you print a list element in Python?

    Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

    How do you print an element of a list in Python without brackets?

    In Python programming language, there are three ways to print a list without brackets..
    Use Python for loop..
    Use * asterisk operator..
    Use Python join() function..

    How do I print a list of elements in a single string in Python?

    To convert a list to a string, use Python List Comprehension and the join() function. The list comprehension will traverse the elements one by one, and the join() method will concatenate the list's elements into a new string and return it as output.

    How do I print a string from a list in Python?

    When you are available with the list containing just the string elements, the best choice is to print the list using the join() function. join() is an in-build python function that takes your list as a parameter and prints it as shown in the below example.

    How do you print a specific element in an array list in Python?

    STEP 1: Declare and initialize an array. STEP 2: Loop through the array by incrementing the value of i. STEP 3: Finally, print out each element of the array.