Multi-Dimensional List In Python

What is List in Python

List is a mutable and sequenced container. It can contain homogeneous objects as well as heterogeneous values such as integers, floats, strings, tuples, lists, and dictionaries. It can be indexed and sliced. A list that has other lists as its element is called the Nested list.

Multi-Dimensional List

Multi-dimensional list

A one-dimensional list is a simple list without any sublists.  A two-dimensional list is a list that solely contains many single-dimensional lists. A three-dimensional list solely contains many two-dimensional lists that solely contains many single-dimensional lists. This concludes that Multi-dimensional lists are the lists within the lists within the lists within the lists and so on.

The one-dimensional list must be a list however it can be an empty list. The numbers of elements in the lists have nothing to do with which dimension the list is. The multi-dimensional lists are mostly used to represent tables, shapes, matrices, etc or anything that concerns digits in multi-dimensional data in Python. A list can be of infinite dimensions in Python however the mainly used multi-dimensional lists are the two-dimensional list and three-dimensional list.

Creating a Multi-Dimensional list using list comprehension

List comprehension method defines a list and its contents at the same time using loops. The basic syntax of list comprehension consists of a square bracket with an expression that is executed for each element along with for loop to iterate over each element.

Accessing elements of  Multi-Dimensional list

Python for loop statement iterates over iterables that are objects which return its element one by one. Sequences are ordered sets of iterables that have specific set of features. Lists, tuples, and strings are all sequences. Multidimensional lists behave just like regular single-dimensional lists.

In python, every element of an ordered sequence is accessed based on their position in the sequence by using square bracket notation[] to index them. To access a list with nested lists, multiple brackets are used. Each bracket corresponds to each dimension of the list. The first bracket corresponds to the outer dimensional list and the second index corresponds to the next dimensional list and so on.


Indexing Multi-Dimensional list

Modifying elements of Multi-Dimensional list

A list is mutable in python, thus its elements can be updated, overloaded, removed, added, appended, extended etc. Modifying an element of a Multi-Dimensional list is just like modifying an element of a normal list.


%d bloggers like this: