# Multi-dimensional lists in Python

## Multi-dimensional lists in Python

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:

 # 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)

Output:

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

Approach 2: Accessing with the help of loop.

 # 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)

Output:

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

Approach 3: Accessing using square brackets.
Example:

 # 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()

Output:

2 4 6 8
1 3 5 7
8 6 4 2
7 5 3 1

## Creating a multidimensional list with all zeros

 # 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)

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:

 # 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)

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.

 # 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)

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.

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

Output:

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

Last Updated on October 24, 2021 by admin

## Find duplicate rows in a Dataframe [With Examples]Find duplicate rows in a Dataframe [With Examples]

When working with large datasets, it is not uncommon to encounter duplicate rows. Duplicates can

## Python Bitwise OperatorsPython Bitwise Operators

Python Bitwise Operators Operators are used to perform operations on values and variables. These are the

## Box plot visualization with Pandas and SeabornBox plot visualization with Pandas and Seaborn

Box plot visualization with Pandas and Seaborn Box Plot is the visual representation of the depicting

## Python – Unzip a list of tuplesPython – Unzip a list of tuples

Python | Unzip a list of tuples The zipping techniques, that is assigning a key-value

## Python – Matplotlib.pyplot ticksPython – Matplotlib.pyplot ticks

Python | Matplotlib.pyplot ticks Matplotlib is an amazing visualization library in Python for 2D plots of

## Python program to convert decimal to binary numberPython program to convert decimal to binary number

Given a decimal number as input, the task is to write a Python program to

## Generating Word Cloud in PythonGenerating Word Cloud in Python

Generating Word Cloud in Python Word Cloud is a data visualization technique used for representing

## Python – Nested List ComprehensionsPython – Nested List Comprehensions

Nested List Comprehensions in Python List Comprehensions are one of the most amazing features of Python.