Explain how to create a multidimensional list.



Explain how to create a multidimensional list...

Answer / chaitanya

There are two ways in which Multidimensional list can be created:

By direct initializing the list as shown below to create multidimlist below

>>>multidimlist = [ [227, 122, 223],[222, 321, 192],[21, 122, 444]]

>>>print multidimlist[0]

>>>print multidimlist[1][2]

__________________________

Output

[227, 122, 223]

192

The second approach is to create a list of the desired length first and then fill in each element with a newly created lists demonstrated below :

>>>list=[0]*3

>>>for i in range(3):

>>> list[i]=[0]*2

>>>for i in range (3):

>>> for j in range(2):

>>> list[i][j] = i+j

>>>print list

__________________________

Output

[[0, 1], [1, 2], [2, 3]]

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Python Interview Questions

Is dictionary faster than list python?

0 Answers  


how do I pass a variable by reference?

0 Answers  


What is difference between input and raw_input in python?

0 Answers  


What are class methods?

0 Answers  


What are closures in python?

0 Answers  


Explain the use of with statement?

0 Answers  


What is the Global Interpreter Lock (GIL) in Python?

1 Answers  


How will you capitalize the first letter of string?

3 Answers  


Are strings mutable in python?

0 Answers  


What is the best python ide for beginners?

0 Answers  


Can I use Python instead of PHP?

0 Answers  


How to pass optional or keyword parameters from one function to another in python?

0 Answers  


Categories