2D List in Python Error | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

2D List in Python Error

The input format: 2 2 2 3 4 5 1st line represents the dimension. 2 is the row. 2 is the column. The next lines represent the elements in the list However, my codes below are wrong n, p = [int(x) for x in input().split()] #n= row, p= col num=[] col=[] for i in range(n): num1, num2=[ float(x) for x in input().split()] col.append(num1) col.append(num2) num.append(col) print(num) How to make a 2D list in Python properly so that the output is: [ [2,3], [4,5] ]

1st Mar 2023, 4:17 AM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
6 Answers
+ 3
for i in range(n): col=[] num1, num2=[ float(x) for x in input().split()] col.append(num1) col.append(num2) num.append(col)
1st Mar 2023, 7:01 AM
Oma Falk
Oma Falk - avatar
1st Mar 2023, 10:34 AM
Per Bratthammar
Per Bratthammar - avatar
+ 3
Hello Khairul, What if 1st number is greater than 1, is it possible? 2 2 2 3 4 5 If it was possible, how the resuting `list` will be?
1st Mar 2023, 6:56 AM
Ipang
+ 3
Ipang Thanks for pointing out my mistake. 😅
1st Mar 2023, 7:35 AM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
+ 3
Per Bratthammar Thanks for sharing👍
2nd Mar 2023, 3:42 AM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
+ 2
Thanks Oma Falk
1st Mar 2023, 7:43 AM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar