print list in python

Python – Print Elements in a List

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

print list 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 * operator to unpack the elements in the list and directly print them, you can use the string join() function to print the list elements as a single string, etc.

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

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 ls 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:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

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 here.

You can also use the * operator to print the list elements. The * 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 sep parameter of the print() 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.

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.

# create a list
ls = [1,2,3,4,5]
# use string join()
print(",".join([str(item) for item in ls]))

Output:

1,2,3,4,5

Here we used a list comprehension to build a list of strings and then applied the string join() 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.

# create a list
ls = [1.456, 2.111, 3.605]
# use string join()
print(",".join(["{:.2f}".format(item) for item in ls]))

Output:

1.46,2.11,3.60

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

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top