6 Ways to Print lists of list in Python

Python List

In this post, we will explore different 6 Ways to Print lists of lists in Python. We will do it without using any external library with some examples that include Print Python list line by line, Using one line code, using Pandas

1. For loop to Print lists of list in Python


To print a Python list or nested lists line by line, we can use for loop to iterate over the lists of the list. Secondly, Printing unpacked all values in nested lists, simply use the print() statement line by line

Python Program to print a Nested List

#program to Print Python List or lists of list

mylist = [[10,12,13],[14,15,16]]

print('printing python list of list:')

for list in mylist:
    print(*list)

The output will be

printing python list of list:
10 12 13
14 15 16

2. Print lists of list Using one line code


We can write the above code in a single line using the list comprehension as shown below example:

Python Program to print lists of list in one line code

#program to  Print Python List or lists of list

original_list = [['Name', 'Age', 'class'],
       ['Rock', '26', 'xth'],
       ['C++', '25', 'Xth']]

[print(*item) for item in original_list][0]



Output

Name Age class
Rock 26 xth
C++ 25 Xth

3. Print List of lists Using Pandas


We can print a list of lists as data frames using the Panda’s library, using the Panda library DataFrame() function we can easily achieve this.

Python Program Example

#program to  Print Python List or lists of list

import pandas as pd


original_list = [['C#', 'Python', '300'],
       ['java', 'Js', '10000'],
       ['C++', 'Go', 'Rust','4000']]
df = pd.DataFrame(original_list)
print(df)

Output

     0       1      2     3
0    C#  Python    300  None
1  java      Js  10000  None
2   C++      Go   Rust  4000

4. Print Python lists of list align Columns


  • First, we are using list comprehension to get the length of each element in nested lists.
  • Using the max() function to find the longest element in the length list.
  • For-loop to iterate over each element of the lists of the list.
  • Finally by Using the str. join() method with str. ljust(width) to get the string output of each row of the table.

Python Program Example

#program to  Print Python List or lists of list

original_list = [['langname', 'lang2', 'lang3'],
       ['java', 'Js', '10000'],
       ['C++', 'Go', 'Rust']]




length_lst = [len(item) for row in original_list for item in row]

col_wdth = max(length_lst)

for row in original_list:    
    print(''.join(item.ljust(col_wdth + 2) for item in row))


Ouput

langname  lang2     lang3     
java      Js        10000     
C++       Go        Rust  

5. map() function to Print lists of list in Python


In this example, we are using Python map() with join() function to print a Python list of list

#program to  Print Python List or lists of list

original_list = [['langname', 'lang2', 'lang3'],
       ['java', 'Js', '10000'],
       ['C++', 'Go', 'Rust']]

list_newline = '\n'.join(' '.join(map(str,sub)) for sub in original_list )

print(list_newline)

Output

langname lang2 lang3
java Js 10000
C++ Go Rust

6. Nested Loop to Print lists of list in Python


In this code example, we are looping over the nested list and using an inner loop to iterate over each element of the sublist and printing it line by line.

#Program to  Print Python List or lists of list

original_list = [['Name', 'Age', 'class'],
       ['Rock', '26', 'xth'],
       ['C++', '25', 'Xth']]

for sublist in original_list:
  for item in sublist:
    print(item,end='  ')
  print('',sep='\n')


Output will be

Name  Age  class  
Rock  26  xth  
C++  25  Xth 

Summary

In this post, We have explored different 6 Ways to print lists of list in Python. without using any external library with some examples that include Print Python list line by line, Using one line code, using Pandas and many moe.