How to take input in Nested list in Python

In this article, we will see how to take input in nested list in Python.
Suppose we want to take an input as shown below:
[['Aby', 21], ['Brandon', 26], ['Dany', 34]]
Let’s see the code first and understand what is that:
my_list = [] temp_list = [] n = 3 for i in range(n): # for number of sub-list in the list for j in range(0,2): # range(2) for size of sub_list temp_list.append(input()) my_list.append(temp_list) temp_list=[] #set temp_list to null print(my_list)
Output:
Aby 21 Brandon 26 Dany 34 [['Aby', 21], ['Brandon', 26], ['Dany', 34]]
First we are creating two lists, one being the main outer list with three elements and other one the inner list with two elements. After every iteration of outer loop set the temporary list null so that no previous values will be present in the temporary list. Finally print the list
Subscribe
Login
Please login to comment
0 Discussion