Related Articles

Related Articles

Multi-dimensional lists in Python
  • Difficulty Level : Medium
  • Last Updated : 09 Dec, 2018

There can be more than one additional dimension to lists in Python. Keeping in mind that a list can hold other lists, that basic principle can be applied over and over. Multi-dimensional lists are the lists within lists. Usually, a dictionary will be the better choice rather than a multi-dimensional list in Python.

Accessing a multidimensional list:

Approach 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate printing
# of complete multidimensional list
a = [[2, 4, 6, 8, 10], [3, 6, 9, 12, 15], [4, 8, 12, 16, 20]]
print(a)

chevron_right


Output:

[[2, 4, 6, 8, 10], [3, 6, 9, 12, 15], [4, 8, 12, 16, 20]]

Approach 2: Accessing with the help of loop.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate printing
# of complete multidimensional list row
# by row.
a = [[2, 4, 6, 8, 10], [3, 6, 9, 12, 15], [4, 8, 12, 16, 20]]
for record in a:
    print(record)

chevron_right


Output:



[2, 4, 6, 8, 10]
[3, 6, 9, 12, 15]
[4, 8, 12, 16, 20]

Approach 3: Accessing using square brackets.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to demonstrate that we
# can access multidimensional list using
# square brackets
a = [ [2, 4, 6, 8 ], 
    [ 1, 3, 5, 7 ], 
    [ 8, 6, 4, 2 ], 
    [ 7, 5, 3, 1 ] ] 
          
for i in range(len(a)) : 
    for j in range(len(a[i])) : 
        print(a[i][j], end=" ")
    print()    

chevron_right


Output:

2 4 6 8 
1 3 5 7 
8 6 4 2 
7 5 3 1
Creating a multidimensional list with all zeros:
filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to create a m x n matrix
# with all 0s
m = 4
n = 5
  
a = [[0 for x in range(n)] for x in range(m)]
print(a)

chevron_right


Output:

[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]

Methods on Multidimensional lists


1. append(): Adds an element at the end of the list.
Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

# Adding a sublist
  
a = [[2, 4, 6, 8, 10], [3, 6, 9, 12, 15], [4, 8, 12, 16, 20]]
a.append([5, 10, 15, 20, 25])
print(a)

chevron_right


Output:

[[2, 4, 6, 8, 10], [3, 6, 9, 12, 15], [4, 8, 12, 16, 20], [5, 10, 15, 20, 25]]

2. extend(): Add the elements of a list (or any iterable), to the end of the current list.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Extending a sublist 
  
a = [[2, 4, 6, 8, 10], [3, 6, 9, 12, 15], [4, 8, 12, 16, 20]]
a[0].extend([12, 14, 16, 18])
print(a)

chevron_right


Output:

[[2, 4, 6, 8, 10, 12, 14, 16, 18], [3, 6, 9, 12, 15], [4, 8, 12, 16, 20]]

3. reverse(): Reverses the order of the list.

filter_none

edit
close

play_arrow

link
brightness_4
code

# Reversing a sublist 
  
a = [[2, 4, 6, 8, 10], [3, 6, 9, 12, 15], [4, 8, 12, 16, 20]]
a[2].reverse()
print(a)

chevron_right


Output:

[[2, 4, 6, 8, 10], [3, 6, 9, 12, 15], [20, 16, 12, 8, 4]]

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :