Python list of lists Code Example

Last Updated On Thursday 20th Jan 2022
  • A list provides a container for zero or more items. (elements).
  • Lists can contain items of different types, including dictionaries and nested lists.
  • A list of lists in Python is a list object where each list element is a list by itself.

list of lists

Example 1

	list_of_lists = ["Hello", "Hi", ["Adam", "Ben", 1, 2, 3], "44", "99"];
	

Example 2

	listoflists = []
a_list = []
for i in range(0, 5):
    a_list.append(i)
    if len(a_list) > 2:
        a_list.remove(a_list[0])
        listoflists.append((list(a_list), a_list[0]))
print(listoflists)
	
	# [([1, 2], 1), ([2, 3], 2), ([3, 4], 3)]