Python Program to Print a List in 3 Easy Ways

Python provides us with a variety of data structures for storing and processing data. One of them is a list.

A Python list is a data structure that holds a mutable sequence of data values. Furthermore, a list is an ordered collection of elements, i.e. they obey the order of the elements.

The Following are the 3 Easy Ways to print the elements of a list given:

  • Using For Loop (Naïve method)
  • Using map() Function
  • Using ‘*’ Symbol

1)Using For Loop (Naïve method)

The Nave method is usually the greatest approach to begin as a starter!

Syntax:

for i in given_list:
    print(i)

Example

Approach:

  • Give the list as static input and store it in a variable.
  • Loop in the given list using the for loop.
  • Inside the for loop, print the element present at the iterator index of the given list.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_listt = [35, 21, 'Python', 87, 58, 'welcome']
print("The All items of the given list = ")
# Loop in the given list using the for loop
for itr in gvn_listt:
    # Inside the for loop, print the element present at the iterator index of
    # the given list.
    print(itr)

Output:

The All items of the given list = 
35
21
Python
87
58
welcome

2)Using map() Function

To quickly print a Python list, combine the map() with join() function.

If the list is not a string, use map() to convert each item to a string and then join them

Syntax:

' '.join(map(str, list))

Approach:

  • Give the list as static input and store it in a variable.
  • Pass str, given list as the arguments to the map() function and join it to an empty string using the join() function and print it.
  • Print the items in a new line by using ‘\n’.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_listt = [35, 21, 'Python', 87, 58, 'welcome']
print("The All items of the given list = ")
# Pass str, given list as the arguments to the map() function and join it to an
# empty string using the join() function and print it
print(' '.join(map(str, gvn_listt)))
print()
# Print the items in a new line
print("Printing the list items in a new line :")
print('\n'.join(map(str, gvn_listt)))

Output:

The All items of the given list = 
35 21 Python 87 58 welcome

Printing the list items in a new line :
35
21
Python
87
58
welcome

Explanation:

The map method is used to convert the values in the list to string format, i.e. we map them to string format.
The elements are then assembled using the join method.

3)Using ‘*’ Symbol

We can also use the ‘*’ symbol to print the list items.

Syntax:

*List

Approach:

  • Give the list as static input and store it in a variable.
  • Print the elements of the given list using the ‘*’ symbol.
  • Print the list items in a new line.
  • By including a sep value, we may customize the output. Set the separation value to a newline using ‘\n’.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_listt = [35, 21, 'Python', 87, 58, 'welcome']
# Print the elements of the given list using the '*' symbol
print("The All items of the given list = ")
print(*gvn_listt)
print()
# Print the list items in a new line
# By including a sep value, we may customize the output.
# Set the separation value to a newline using '\n'.
print("Printing the list items in a new line :")
print(*gvn_listt, sep="\n")

Output:

The All items of the given list = 
35 21 Python 87 58 welcome

Printing the list items in a new line :
35
21
Python
87
58
welcome