Print lists in Python (4 Different Ways)

In Python, four kinds of inherent information types are utilized to store numerous components as an assortment. These are records, tuples, sets, and word references. Here, records are utilized all the time by any client. As records as of now store information in the grouping, there are various approaches to printing them to make them look more adequate and simple to peruse.

Python programming has different in-constructed information designs to make the programming quicker and more proficient like some other programming languages. This multitude of information structures is consecutive in nature and stores enormous information assortment in different configurations, in contrast to other people. The rundown is one of those python information structures that are basic yet strong and stores a wide scope of the information under a solitary variable. In this article, we will concentrate on different strategies to print a rundown in python with models and results.

Yet, before that, let us have a short prologue to the python list.

Python gives a rundown structure to store numerous things in a solitary variable. These factors can be various sorts like string, number, object, and so on A rundown can be printed by involving various courses in Python. Printing a rundown implies placing things esteems into the standard result like a terminal, site page, or record. The rundown is basically the same as the cluster in other programming dialects and printing the rundown activity is additionally called a printing exhibit in Python.

What are Lists in Python?

The rundown is one of the key information structures used to store numerous things in a solitary variable. You can make a rundown by setting components between the square brackets([]) isolated by commas (,). A rundown can contain quite a few components of various information types like number, float, string, and so forth Records contain countless properties which make them not the same as different information structures. Allow us to see a portion of those properties beneath:

Properties of the rundown:

  • Requested: The things inside the rundown have a characterized request, and that request won’t change.
  • Dynamic: The size of a powerful rundown can be progressively changed at runtime for example we can undoubtedly add or eliminate a thing from the rundown during run time without determining any size.
  • Variable: The components of the rundown can be altered, individual components can be supplanted, and the request for components can be changed even after the rundown has been made.
  • Ordering: List components can be gotten to involving a mathematical file in square sections. List ordering is likewise zero-based.
  • Can contain copies: The rundown can contain copy components.

The least complex rundown is the vacant rundown with square sections however no components inside it ([]). It is significant that the vacant rundown assesses to False when utilized in a boolean setting. Likewise, a rundown can likewise have one more rundown as a component, well known as a settled rundown.

Print lists in Python (4 Different Ways)

1- Using for loop: Traverse from 0 to len(list) and print all elements of the list one by one using a for loop, this is the standard practice of doing it.

# Python program to print list
# using for loop
a = [1, 2, 3, 4, 5]
 
# printing the list using loop
for x in range(len(a)):
    print a[x],

Output:

1 2 3 4 5

2- Without using loops: * symbol is use to print the list elements in a single line with space. To print all elements in new lines or separated by space use sep=”\n” or sep=”, ” respectively.

# Python program to print list
# without using loop
 
a = [1, 2, 3, 4, 5]
 
# printing the list using * operator separated 
# by space 
print(*a)
 
# printing the list using * and sep operator
print("printing lists separated by commas")
 
print(*a, sep = ", "
 
# print in new line
print("printing lists in new line")
 
print(*a, sep = "\n")

Output:

1 2 3 4 5
printing lists separated by commas
1, 2, 3, 4, 5
printing lists in new line
1
2
3
4
5

3- Convert a list to a string for display: If it is a list of strings we can simply join them using join() function, but if the list contains integers then convert it into string and then use join() function to join them to a string and print the string.

# Python program to print list
# by Converting a list to a 
# string for display
a =["Geeks", "for", "Geeks"]
 
# print the list using join function()
print(' '.join(a))
 
# print the list by converting a list of 
# integers to string 
a = [1, 2, 3, 4, 5]
 
print str(a)[1:-1

Output:

1, 2, 3, 4, 5

4- Using map: Use map() to convert each item in the list to a string if list is not a string, and then join them:

# Python program to print list
# print the list by converting a list of 
# integers to string using map
 
a = [1, 2, 3, 4, 5]
print(' '.join(map(str, a))) 
 
print"in new line"
print('\n'.join(map(str, a)))

Output:

1 2 3 4 5
in new line
1
2
3
4
5

Also Read: How to Install OpenCV for Python on Windows?

Leave a Reply

Your email address will not be published.