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) # Output: 1
You can also modify elements of the array using indexing:
# Modifying an element of the array my_array = 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.
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
Most Viewed Articles (in Python )
Latest Articles (in Python)
- Data Science
- React Native
- Cloud Computing
- Java Beans
- Mac OS X
- Office 365
- Tech Reviews