- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How do I create a multidimensional list in Python?
Multidimensional Lists are Lists within Lists. The left index as row number and right index as column number, for example
list[r][c]
Above, r is row number and c is column number.
Let’s see an example. For multidimensional list 2x3 −
list [2][3]
Create a Multidimensional Python List
Example
In this example, we will learn how to create a Multidimensional List in Python. We will also iterate and print the array.
# Create a Multi-Dimensional Python List mylist = [[2,5], [10,24,68], [80]] print("\nMultidimensional List") for outList in mylist: print(outList)
Output
Multidimensional List [2, 5] [10, 24, 68] [80]
How to access elements in a Multidimensional Python List
Example
In this example, we will learn how to access elements in a multidimensional list. Access using square brackets −
# Create a Multi-Dimensional Python List mylist = [[2,5], [10,24,68], [80]] print("\nMultidimensional List") for outList in mylist: print(outList) # Accessing Third element in the 2nd row print("\nThird element in the 2nd row = \n",mylist[1][2])
Output
Multidimensional List [2, 5][10, 24, 68] [80] Third element in the 2nd row = 68
Append elements in a Multidimensional List
Example
The append() method is used to append elements in a Multidimensional List. The elements added will get appended i.e. in the end −
# Create a Multi-Dimensional Python List mylist = [[2,5], [10,24,68], [80]] print("Multidimensional List") for outList in mylist: print(outList) # Append elements mylist.append([65, 24]) print("\nDisplaying the updated Multidimensional List") for outList in mylist: print(outList)
Output
Multidimensional List [2, 5] [10, 24, 68] [80] Displaying the updated Multidimensional List [2, 5] [10, 24, 68] [80] [65, 24]
Advertisements