To create a list of lists in Python, you can use list comprehension or the `append` method to add lists as elements to create inner lists.
In this example,
list1
and list2
.listOfLists
and initialize it with the inner lists.list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
listOfLists = [list1, list2]
print('List of lists:', listOfLists)
List of lists: [[1, 2, 3], ['a', 'b', 'c']]
In this example,
listOfListsEmpty
.list3
and list4
.append
method to add the inner lists list3
and list4
to the outer list listOfListsEmpty
.listOfListsEmpty = []
list3 = []
list4 = []
listOfListsEmpty.append(list3)
listOfListsEmpty.append(list4)
print('List of lists with empty inner lists:', listOfListsEmpty)
List of lists with empty inner lists: [[], []]
In this tutorial, we learned How to create a List of Lists in Python language with well detailed examples.