This tutorial guides you on how to create list of lists in Python programming language using dynamic lists and simple lists. Also you will learn how to access item from list of lists.
Create list of lists – dynamic
In the following example, you will be creating two dynamic lists and then create list of lists (dynamic lists) in the for loop.
listOfLists =  myfirstlist =  mysecondlist =  n = 10 for i in range (0,n): myfirstlist.append(i) mysecondlist.append(n-i) if len(myfirstlist) > 3 and len(mysecondlist) > 3: myfirstlist.remove(myfirstlist) mysecondlist.remove(mysecondlist) listOfLists.append((list(myfirstlist), list(mysecondlist))) print (listOfLists)
Note, the lists myfirstlist, mysecondlist are created dynamically in for loop in a way we wanted as shown in the output. After that you create a copy of whole list using the list constructors list(myfirstlist) and list(mysecondlist).
Finally, use append() method of list to create list of lists with the following command.
[([1, 2, 3], [9, 8, 7]), ([2, 3, 4], [8, 7, 6]), ([3, 4, 5], [7, 6, 5]), ([4, 5, 6], [6, 5, 4]), ([5, 6, 7], [5, 4, 3]), ([6, 7, 8], [4, 3, 2]), ([7, 8, 9], [3, 2, 1])]
How to access items from list of lists ?
You can access items from list of lists. For example, to access [8,7,6], you need to use indexes appropriately as shown below.
>>> print(listOfLists) [8, 7, 6]
List of lists creation – Simple
In the above section we have seen how to create dynamic list of lists. If you have predefined list as shown below: list1 and list2, you can use simply append() method to create list of lists.
list1 = [10,20,30,40] list2 = [11,22,33,44] listOfLists =  listOfLists.append(list1) listOfLists.append(list2) print('list of lists: ', listOfLists)
list of lists: [[10, 20, 30, 40], [11, 22, 33, 44]]
Similarly, to access second element from the second list in the list of lists:
#2nd element from 2nd list print(listOfLists) 22
That’s it. You had learnt how to form list of lists with simple and complex/dynamic lists.
Hope it is helpful 🙂
You’ll also like:
- String Formatting in Python – Examples
- Single digit number to double digits string conversion in Python
- String Immutability with Examples in Python
- Python Program to Check Given Number is Odd or Even
- Increase the cell width of the Jupyter Notebook in browser
- Add python3 kernel to jupyter IPython notebook ?
- Programs to Print Patterns – Pyramid, Triangle using Star
- How to change the default theme in Jupyter Notebook ?
- Change the Jupyter Notebook startup folder in Windows & Mac
- To run Jupyter Notebook on Windows from command line
- String Slicing in Python with Examples
- Python program to find the greatest of three numbers
- Find difference between two given numbers in Python
- String indexing in Python with Examples
- Embed HTML within IPython Notebook
- Remove non-numeric characters from string in Python
- Install Python 3 on Windows 10 machine
- Convert negative to positive number in Python
- TypeError: a bytes-like object is required, not ‘str’ – Python3
- Extract numbers from a string in python
- How to access index and value in for loop – Python?
- Python Lists with Examples
- Convert list of lists to flat list – flatten list in Python