Programming Tutorials

Multidimensional list (array) in python

By: Koothrapalli in Python Tutorials on 2012-04-07  

In Python, a multidimensional list is a list of lists, where each list represents a row of the array and contains a collection of values.

Here's an example of creating a two-dimensional list (array) in Python:

# Creating a 2D array
my_array = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

This creates a 2D array with three rows and three columns. You can access elements of the array using two indices, one for the row and one for the column:

# Accessing an element of the array
print(my_array[0][0])  # Output: 1

You can also modify elements of the array using indexing:

# Modifying an element of the array
my_array[1][1] = 10

You can iterate over a multidimensional list using nested loops:

# Iterating over a 2D array
for i in range(len(my_array)):
    for j in range(len(my_array[i])):
        print(my_array[i][j])

This will print out each element of the array.

You can create arrays with more than two dimensions by nesting additional lists:

# Creating a 3D array
my_array = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]]

In general, multidimensional lists are a powerful tool for working with structured data in Python. They can be used to represent matrices, tables, and other data structures with rows and columns.






Add Comment

* Required information
1000

Comments

No comments yet. Be the first!

Most Viewed Articles (in Python )

Latest Articles (in Python)