Create a List of Lists in Python

  • Post category:List

A list of lists is a nested list that contains other lists as its elements. We can create a list of lists in Python by simply including lists inside a parent list. Here are two examples:

Example 1:

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

Output:

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

Example 2:

list_of_lists = [['apple', 'banana'], ['carrot', 'potato', 'onion'], ['chicken', 'beef', 'pork', 'fish']]
print(list_of_lists)

Output:

[[‘apple’, ‘banana’], [‘carrot’, ‘potato’, ‘onion’], [‘chicken’, ‘beef’, ‘pork’, ‘fish’]]