How to Print a List of Lists in Python?

Estimated read time 1 min read

To print a list of lists in Python, you can use nested loops to iterate through each list and each element within each list. Then, you can print each element using the print() function.

Here is an example:

list_of_lists = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

for inner_list in list_of_lists:
    for element in inner_list:
        print(element, end=" ")
    print()

In this example, we have a list of lists called list_of_lists. We use a for loop to iterate through each inner list, and then use another for loop to iterate through each element in the inner list. We print each element using the print() function, and use the end parameter to separate the elements with a space. We then print a newline character to move to the next line and print the elements of the next inner list.

The output of this code will be:

1 2 3 
4 5 6 
7 8 9 

Note that you can modify the print() function to customize the separator between elements or the end of each line. For example, you can use print(element, end="|") to separate the elements with a pipe character instead of a space.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply