Print list for python

How to Print a List in Python

To print the contents of a list in Python, all you need to do is call the print() function on it.

Here is an example of printing a list of three fruits in a list:

fruits = ["Banana", "Apple", "Orange"] print(fruits)

But if you want to print each element of a list separately, you should use a for loop.

For instance, let’s print the three elements of a list on separate lines by using a for loop:

fruits = ["Banana", "Apple", "Orange"] for fruit in fruits: print(fruit)

These answers are probably enough, but in case you’re interested, feel free to keep on reading to learn more approaches to printing lists in Python.

Читайте также:  Java logging log file name

Alternative Ways to Print a List in Python

Besides passing the list to a print function or using a loop to print each element, there are three great alternative ways to print list items in Python.

Let’s take a closer look at these approaches and why you might want to consider those.

1. Use an Asterisk to Print a List in Python

To print a list without commas or square brackets without using a loop, you can use the asterisk to unpack the list elements.

fruits = ["Banana", "Apple", "Orange"] print(*fruits)

The asterisk operator in Python is known as the unpacking operator. You can use it to pull values from iterables, such as lists, strings, or tuples.

And by the way, if you want to have a separator between the printed list elements, specify sep parameter in the print() function call.

For instance, let’s print the list of elements using the unpacking operator and separate the elements by a comma and a blank space:

fruits = ["Banana", "Apple", "Orange"] print(*fruits, sep=", ")

Notice that the separator can be anything.

For example, you can use this approach to print each element on a new line by:

fruits = ["Banana", "Apple", "Orange"] print(*fruits, sep="\n")

2. Use the Built-In join() Method for Lists of Strings

If you have a list of strings, you can use the string.join() method to combine the strings and print the result.

fruits = ["Banana", "Apple", "Orange"] print(" ".join(fruits))

Notice that the .join() method belongs to the string type. So this does not work for example when the list consists of integers.

3. Use the Built-In Map Function to Print a List in Python

To print a list of integers similar to how you printed the strings in the previous example:

  1. Use the map() function to transform them into strings.
  2. Call join() method to merge them into one string.
  3. Print the resulting string out using the print() function.
nums = [1, 2, 3, 4, 5] print(" ".join(map(str, nums)))

Let’s take a quick look at how this code works.

  1. The map() function creates a new list that contains each integer as a string.
  2. Then the join() method combines all the strings in the new list into one string that is printed out.

Conclusion

That’s a whole bunch of ways to print lists in Python.

Before you go, here is a quick recap of the things to take home.

The easiest way for you to print a list in Python is by just calling print() function on a list:

fruits = ["Banana", "Apple", "Orange"] print(fruits)

To separately print each element, use a for loop:

fruits = ["Banana", "Apple", "Orange"] for fruit in fruits: print(fruit)

Alternatively, you can use

  • String’s join() method.
  • Asterisk unpacking (*).
  • The map() function with the join() method.

Thanks for reading. I hope you find it useful.

Happy coding!

Further Reading

1 thought on “How to Print a List in Python”

I may need your help. I’ve been doing research on gate io recently, and I’ve tried a lot of different things. Later, I read your article, and I think your way of writing has given me some innovative ideas, thank you very much.

Leave a Comment

About the Author

Hi, I’m Artturi Jalli!

I’m a Tech enthusiast from Finland.

I make Coding & Tech easy and fun with well-thought how-to guides and reviews.

I’ve already helped 5M+ visitors reach their goals!

ChatGPT Review (and How to Use It)—A Full Guide (2023)

ChatGPT is the newest Artificial Intelligence language model developed by OpenAI. Essentially, ChatGPT is an AI-based chatbot that can answer any question. It understands complex topics, like.

10 Best AI Art Generators of 2023 (Reviewed & Ranked)

Choosing the right type of AI art generator is crucial to produce unique, original, and professional artwork. With the latest advancements in AI art generation, you can.

How to Make an App — A Complete 10-Step Guide (in 2023)

Are you looking to create the next best-seller app? Or are you curious about how to create a successful mobile app? This is a step-by-step guide on.

9 Best Graphic Design Courses + Certification (in 2023)

Do you want to become a versatile and skilled graphic designer? This is a comprehensive article on the best graphic design certification courses. These courses prepare you.

8 Best Python Courses with Certifications (in 2023)

Are you looking to become a professional Python developer? Or are you interested in programming but don’t know where to start? Python is a beginner-friendly and versatile.

8 Best Swift & iOS App Development Courses [in 2023]

Are you looking to become an iOS developer? Do you want to create apps with an outstanding design? Do you want to learn to code? IOS App.

Источник

3 Ways to Print a List in Python [Step-by-Step]

Printing List In Python

Printing lists in Python is a fundamental operation that allows developers to display list elements individually.

This can be useful in many situations. For instance, suppose you’re building a social media application that contains all of a user’s friends in a Python list. In that case, you’ll need to print the list to display them. This is just an example, the reason can be more such as printing the list’s elements for debugging, sharing data, outputting results, etc.

This tutorial will show you various techniques and methods to print all the elements of a list in Python. Let’s start with a short introduction to the list.

Introduction to Python List

Python offers us various in-built data structures to make storing and processing data much easier. A list is one of them.

Python List is a typed array that can store duplicated elements, it is an ordered collection of data, dynamic in size, and mutable, and its elements can be accessed using list indexing.

Since there is an ordered data collection, it becomes easy to print the contents of a list. Python has a lot to offer for this, let’s look at them one by one.

Python map() function can be clubbed with the join() function to print the Python list elements individually.

Here, inside ‘ ‘, you can insert /n if you want to print each element of a list on the next line, or you can insert a comma(,) so that element is in one line separated by commas, or you can use any custom symbols.

lst = [10,20,30,'John',50,'Joe'] print("Elements of List:\n") print('\n'.join(map(str, lst)))

Here we used the map() function to convert the integer & string data types present in the list to the string using str() as the mapping function. Then, we used the join() function to combine the elements into a single string with a newline character (‘\n’) used as the separator between list items.

Elements of List: 10 20 30 John 50 Joe

In the above output, you can see that each element of the list is printed on a new line, hence the operation is successful.

To make the process easier, we can use the * symbol to unpack the list elements and print it further. Let’s see how.

We can customize the output by including the sep value.

lst = [10,20,30,'John',50,'Joe'] print("Elements of List:\n") print(*lst, sep = "\n")

Here we have passed sep = “\n” so that each element will be printed in a new line like in the previous example.

Elements of List: 10 20 30 John 50 Joe

Here we got the same output as above but this method is shorter and easier than the previous one as we use just one symbol whereas, in the previous method, we have to use three methods.

As a beginner, the naive approach is always the best to start with. In this method, we iterate over each element of the list one by one using for loop and print the element parallelly.

for element in list: print(element)
lst = [10,20,30,'John',50,'Joe'] print("Elements of List:\n") for x in lst: print(x)

Here, for loop will iterate through the list one by one, and print the element on each iteration. For this method, each element will automatically be printed on a new line, so nothing extra is needed.

Elements of List: 10 20 30 John 50 Joe

In the above output, we got the same result as the previous results. Still, this approach seems simple and easy to understand for beginners as no additional parameters or methods are required here.

Conclusion

In this tutorial, we looked at three ways to print a Python list: using map(), using the * symbol, and using a for loop. All of them can be used accordingly, but if you want the simplest way that doesn’t use an extra concept then it’s best to use a traditional for loop. Hope you got enough information to be able to print a list of elements in Python.

References

Источник

Оцените статью