How to print elements in a list 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 to print elements in a list 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.

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

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.

Using * operator to unpack the list

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.

Using string join() 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.

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

In Python, four types of built-in data types are used to store multiple elements as a collection. These are lists, tuples, sets, and dictionaries. Here, lists are used very often by any user. As lists already store data in sequence, there are different ways of printing them to make them look more presentable and easy to read.

This tutorial will demonstrate different ways of printing a list in Python.

Use the map() Function to Print Lists in Python

The map() function is a built-in feature in Python. This command, also known as mapping, is used to manipulate all the elements in an iteration or a sequence without using any kind of loop. This function basically converts one type of iterable into another type. See the example below.

list = [5, 10, 15, 20, 25] print(list) print("After using the mapping technique: ") print('\n'.join(map(str, list)))

Output:

[5, 10, 15, 20, 25] After using the mapping technique: 5 10 15 20 25

In the program above, note that the join() method was implemented. The join() function in Python is used to join elements of any iterable with the help of a string separator. The string separator used above is \n,, which is the new line character used to denote the end of a line. It’s why every element is in a different line in the output.

Use the * Operator to Print Lists in Python

The * operator is the most commonly used operator of the many operators present in Python. Except for performing multiplication, the * operator is used to print each element of a list in one line with a space between each element.

Along with the * operator, the new line character \n can also be used with the help of the sep = parameter in the print statement itself. The sep = parameter basically provides a separator between the strings. Check out the sample code below.

list = [5, 10, 15, 'Twenty', 25] print(list) print("After using the * operator: ") print(*list)

Output:

[5, 10, 15, 'Twenty', 25] After using the * operator: 5 10 15 Twenty 25

The new line character \n can be used with the help of sep = in the last print statement after placing a comma after *list.

Use a for Loop to Print Lists in Python

The for loop is commonly used in any programming language. It is used to iterate over a sequence like a tuple, a dictionary, a list, a set, or a string and execute for each and every element present in the sequence.

Example:

list = [5, 10, 15, 'Twenty', 25] print("After using for loop:") for l in list: print(l)

Output:

[5, 10, 15, 'Twenty', 25] After using for loop: 5 10 15 Twenty 25

Here, the for loop executes over each and every element present in the given list.

Use the join() Method to Print Lists in Python

The join() function in Python is used to join elements of any iterable like a list, a tuple, or a string with the help of a string separator; this method returns a concatenated string as an output. Look at the example below.

list =['Five', 'Ten', 'Fifteen', 'Twenty'] print(' '.join(list))

Output:

Five Ten Fifteen Twenty

You can only use this process when there are strings present in the list.

DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Python List

  • Convert a Dictionary to a List in Python
  • Remove All the Occurrences of an Element From a List in Python
  • Remove Duplicates From List in Python
  • Get the Average of a List in Python