| by Arround The Web | No comments

Python Print List

Python’s ability to work with lists is an essential feature. “Lists” in Python correspond to the ordered collections of items/elements of any type. “Lists” are utilized in Python code for storing and manipulating data, such as numbers, strings, or objects. To print the lists and display the contained elements, various methods are used in Python.

This write-up will give an in-depth guide on printing a Python list using appropriate examples.

How to Print Python List?

The following methods are utilized to print the Python list:

Method 1: Print Python List Using the “for” Loop

The “for” loop is utilized to print a Python list via iteration.

Example

Here is the code that utilizes the “for” loop to display the Python list:

list_value = ['Python', 'Java', 'Linux']
print('Given List: ', list_value)
for i in range(0, len(list_value)):
     print(list_value[i])

 
In the above code:

    • The “range()” function takes the start range “0” and end range “len(list_value)” to create the specified range. Here, the “len()” function accepts the initialized list to determine its total length.
    • Lastly, the “for” loop iterates over the range and displays each list element.

Output


The defined list has been printed on the console.

Method 2: Print Python List Utilizing the “*” Operator

The “*” operator is utilized to unpack the sequence, such as list, tuple, set, etc. This unpacking operator “*” can also be used to print the Python list with a separator value.

Example

The below code is used to print the list:

list_value = ['Python', 'Java', 'Linux']
print('Given List: ', list_value)
print(*list_value, sep='\n')

 
According to the above code lines, the “*” operator is used to unpack the list and print all the elements of the list.

Output


The defined list elements have been printed successfully.

Method 3: Print Python List Utilizing the “join()” Method

The “join()” method is used to accept all items/elements of the input iterable and join them into an empty string with a particular separator value.

Syntax

string.join(iterable)

 
Example

The following example code prints a Python list:

list_value = ['Python', 'Java', 'Linux']
print('Given List: ', list_value)
print('\n'.join(list_value))

 
In this code, the “join()” method takes the defined list as an argument and joins it with a separator value “\n” such that all the list elements are printed.

Output


The input list elements have been printed successfully.

Method 4: Print Python List Utilizing “List Comprehension”

The “List Comprehension” approach creates a list based on the existing elements. We can use this approach to display Python list items/elements.

Example

The below-given code is used to print the Python list:

list_value = ['Python', 'Java', 'Linux']
print('Given List: ', list_value)
[print(i) for i in list_value]

 
In the above code, the “List Comprehension” approach and the “for” loop are applied to iterate through/over the particular list and print all the list elements.

Output


The input list elements have been printed successfully.

Conclusion

The “for” loop, “*” operator, “List Comprehension” approach, or the “join()” method is employed to print the list in Python. The simplest among these approaches is the “for” loop that iterates over the list and prints each element conveniently. However, the other methods, such as the “* operator” and “join()” methods and others, are also effective. This write-up presented multiple ways of printing Python lists using appropriate examples.

Share Button

Source: linuxhint.com

Leave a Reply