How to Print Lists in Python?

As a developer working on a project for one of my clients in New York, I recently encountered a situation where I needed to display the contents of a list in a clear and readable format. After exploring various methods, I discovered several effective ways to print lists in Python. In this tutorial, I will explain how to print lists in Python with examples.

Python Lists

Before getting into printing lists, let’s quickly recap what lists are in Python. A list is a built-in data structure that allows you to store multiple items in a single variable. Lists are ordered, mutable, and can contain elements of different data types. Here’s an example of a list:

us_cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"]

Read How to Convert a List to a Set in Python?

Print Lists in Python

The simplest way to print a list in Python is by directly passing the list variable to the print() function. For example:

us_cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"]
print(us_cities)

Output:

['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix']

You can see the output in the screenshot below.

Print Lists in Python

This method works well for small lists, but the output can become unclear for larger lists or when you need more control over the formatting.

Check out How to Generate a List of Random Numbers in Python?

Use Loops

To print list elements on separate lines or with custom formatting, you can use loops. Python provides two commonly used loops: for loop and while loop in Python.

1. Use a for Loop

A for loop allows you to iterate over each element in a list and print them individually. Here’s an example:

us_states = ["California", "Texas", "Florida", "New York", "Illinois"]
for state in us_states:
    print(state)

Output:

California
Texas
Florida
New York
Illinois

You can see the output in the screenshot below.

How to Print Lists in Python

You can customize the output by adding additional formatting or text within the loop.

Read How to Slice Lists in Python?

2. Use a while Loop

Alternatively, you can use a while loop to print Python list elements. This approach is useful when you need more control over the iteration process. Here’s an example:

us_presidents = ["George Washington", "John Adams", "Thomas Jefferson", "James Madison", "James Monroe"]
index = 0
while index < len(us_presidents):
    print(f"{index + 1}. {us_presidents[index]}")
    index += 1

Output:

1. George Washington
2. John Adams
3. Thomas Jefferson
4. James Madison
5. James Monroe

You can see the output in the screenshot below.

Print Lists in Python while loop

In this example, we use the index variable to keep track of the current position in the list and print the elements along with their corresponding numbers.

Check out How to Concatenate a List of Strings into a Single String in Python?

Advanced Printing Techniques

Python offers several advanced techniques for printing lists that provide more flexibility and formatting options.

1. Use the join() Method

Python join() method allows you to concatenate list elements into a single string, with a specified delimiter. This is particularly useful when you want to print list elements separated by a specific character or string. Here’s an example:

us_tech_companies = ["Apple", "Microsoft", "Amazon", "Google", "Facebook"]
print(", ".join(us_tech_companies))

Output:

Apple, Microsoft, Amazon, Google, Facebook

You can see the output in the screenshot below.

Print Lists in Python join() Method

In this case, the list elements are joined together using a comma and space as the delimiter.

Check out How to Iterate Through a List Backward in Python?

2. Use List Comprehension

Python list comprehension is a concise way to create new lists based on existing lists. You can use list comprehension to print list elements with custom formatting. Here’s an example:

us_rivers = ["Mississippi", "Missouri", "Colorado", "Ohio", "Columbia"]
[print(f"The {river} River") for river in us_rivers]

Output:

The Mississippi River
The Missouri River
The Colorado River
The Ohio River
The Columbia River

This code uses list comprehension to print each river name with the added text “The” and “River”.

Read How to Clear a List in Python?

Print Lists with Conditional Filtering

Sometimes, you may want to print only certain elements from a list based on a specific condition. You can achieve this by combining loops and conditional statements. Here’s an example:

us_cities = ["New York", "Los Angeles", "Chicago", "Houston", "Phoenix"]
for city in us_cities:
    if len(city) > 7:
        print(city)

Output:

New York
Los Angeles
Chicago
Houston

In this example, only the cities with names longer than 7 characters are printed.

Check out How to Check if an Element is Not in a List in Python?

Example

To illustrate the practical application of printing lists in Python, let’s consider a real-world scenario. Suppose you are working on a project that involves analyzing sales data for a US-based company. You have a list of sales figures for different regions, and you need to display the data in a readable format.

sales_data = [
    {"region": "Northeast", "sales": 250000},
    {"region": "Southeast", "sales": 180000},
    {"region": "Midwest", "sales": 220000},
    {"region": "West", "sales": 300000}
]

print("Sales Report:")
for entry in sales_data:
    region = entry["region"]
    sales = entry["sales"]
    print(f"{region}: ${sales:,}")

Output:

Sales Report:
Northeast: $250,000
Southeast: $180,000
Midwest: $220,000
West: $300,000

In this example, we have a list of dictionaries representing sales data for different regions. By iterating over the list and accessing the relevant keys, we can print the sales report in a clear and formatted manner.

Read How to Add Elements to an Empty List in Python?

Conclusion

In this article, I explained how to print lists in Python. I discussed Python and printing lists using loops, such as the for loop and the while loop. Advanced techniques like join(), list comprehension, and conditional filtering. I also gave a real-world example to demonstrate how printing lists can be applied in a practical scenario.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.