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?
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:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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.
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.