0
row=int(input("Number of rows for two dimensional list please:")) 
print("Enter",row,"rows as a list of int please:")

numbers = []
for i in range(row):
    numbers.append(input().split())
array=[0]*row
for i in range(row):
    array[i]=[numbers]



print(array)

The program inputs are

1 2 3
4 5 6
7 8 9

This program output:

[[[['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9']]], [[['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9']]], [[['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9']]]]

How can I turn into this 2 dimensional array like this

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

1 Answer 1

1

Try using a list comprehension, and iterate trough the lines, using splitlines, then split the lines, then convert the values to an integer:

row=input("Number of rows for two dimensional list please:")
print([list(map(int,i.split())) for i in row.splitlines()])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.