Answers for "2d lists python"

1

how to input 2-d array in python

matrix = [input().split() for i in range(no_of_rows)] # if only row is given and the number of coloumn has to be decide by user
matrix= [[input() for j in range(no_of_cols)] for i in range(no_of_rows)] # if both row and coloumn has been taken as input from user
Posted by: Guest on April-27-2020
4

how to create 2d list in python

o=[]
for i in range(0,rows):
    x=[]
    for j in range(0,cols):
        x.append(0)
    o.append(x)
#if you use [[0]*cols]*rows all rows will become the same list
#so editing in one row will edit all rows
Posted by: Guest on November-11-2020
5

create a 2d array in python

def build_matrix(rows, cols):
    matrix = []

    for r in range(0, rows):
        matrix.append([0 for c in range(0, cols)])

    return matrix

if __name__ == '__main__':
    build_matrix(6, 10)
Posted by: Guest on February-27-2020
2

2d array python3

# 5x6, 2-d array of booleans using list comprehension:

matrix = [[False for col in range(6)] for row in range(5)]

# 6x5, 2-d array of banana's using list comprehension:

matrix = [['banana' for col in range(5)] for row in range(6)]
Posted by: Guest on June-07-2020
0

how to create one list from 2d list python

>>> l = [[1, 2, 3], [4, 5, 6], [7], [8, 9]]
>>> sum(l, [])
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Posted by: Guest on October-24-2021
0

We pass in a 2 dimensional list. You should output the 3rd element of the 2nd row.

In a 2 dimensional list, output the 3rd element of the 2nd row.
# remember to account for 0 index
print(variable[1][2])
Posted by: Guest on January-30-2020

Python Answers by Framework

Browse Popular Code Answers by Language