Python : Two dimensional list

A two dimensional list can be created as a list within a list. Below are some of the operations that can performed on a two dimensional list.

Declare a two dimensional list

The syntax for declaring a two dimensional list is as below

2d_list = [ [ item1, item2, item3 ], [ element1, element2, element3 ] ]

Python3 : Creating a two dimensional list

# Declare a two dimensional list
_list_2d = [ [ 'G', 'O', 'T' ], [ 'l', 'o', 't', 'r' ], [ 1, 2, 3 ] ] 

# Print each list in a two dimensional list
print ("Each list in a two dimensional list")
for _list in _list_2d:
    print (_list)

# Get individual list from a two dimensional list
print ("Individual list using index...")
print (_list_2d[0])
print (_list_2d[2])

Output

Each list in a two dimensional list
['G', 'O', 'T']
['l', 'o', 't', 'r']
[1, 2, 3]
Individual list using index...
['G', 'O', 'T']
[1, 2, 3]


Inserting in a two dimensional list

To insert a list in a two dimensional list, we could use an insert ( position, item ) function

Python3 : Inserting in a two dimensional list

# Declare a two dimensional list
_list_2d = [ [ 'Game', 'Of', 'Thrones' ], [ 'lord', 'of', 'the', 'rings' ] ] 

# Print each list in a two dimensional list
print ("Two dimensional list before insert")
for _list in _list_2d:
    print (_list)

_list_2d.insert(0, ['You', 'know', 'who'])
_list_2d.insert(3, ['Must', 'not', 'be', 'named'])

# Inserting a list in a two dimensional list
print ("Two dimensional list after insert...")
for _list in _list_2d:
    print (_list)

Output

Two dimensional list before insert
['Game', 'Of', 'Thrones']
['lord', 'of', 'the', 'rings']
Two dimensional list after insert...
['You', 'know', 'who']
['Game', 'Of', 'Thrones']
['lord', 'of', 'the', 'rings']
['Must', 'not', 'be', 'named']


Change items in a two dimensional list

A two dimensional list in python could be changed or updated using the index of the element i.e using [ ] [ ] syntax.

Python3 : Modify items in a two dimensional list

# Declare a two dimensional list
_list_2d = [ [ 'Star', 'Craft', 'Static' ], [ 'Age', 'Of', 'Empires' ] ] 

# Print each list in a two dimensional list
print ("Two dimensional list...")
for _list in _list_2d:
    print (_list)

# Modify individual elements in a 2 dimensional list
_list_2d[0][2] = 'Expansion'
_list_2d[1][1] = 'Of the great'

print ("Two dimensional list after modification...")
for _list in _list_2d:
    print (_list)

Output

Two dimensional list...
['Star', 'Craft', 'Static']
['Age', 'Of', 'Empires']
Two dimensional list after modification...
['Star', 'Craft', 'Expansion']
['Age', 'Of the great', 'Empires']


Deleting items in a two dimensional list

An item in a two dimensional list in python could be deleted using the del _list [ index ].
Note : After each deletion using del function, the indices of the remaining items are updated.
See below example

Python3 : Deleting items in a two dimensional list

# Declare a two dimensional list
_list_2d = [ [ 'A', 'New', 'Beginning' ], [ 'Right', 'Here' ], ['ABC', 'XYZ'] ] 

# Print each list in a two dimensional list
print ("Two dimensional list...")
for _list in _list_2d:
    print (_list)

# Delete individual elements in a 2 dimensional list
del _list_2d[0][0] # After deletion, the 0'th item in the 2d list becomes [ 'New', 'Beginning' ] 
del _list_2d[0][1] # This would delete item at index 1 in the first list i.e 'Beginning'.
del _list_2d[2]    # This would delete the third item in the two dimensional list

print ("Two dimensional list after deletion of items...")
for _list in _list_2d:
    print (_list)

Output

Two dimensional list...
['A', 'New', 'Beginning']
['Right', 'Here']
['ABC', 'XYZ']
Two dimensional list after deletion of items...
['New']
['Right', 'Here']


Copyright (c) 2019-2021, Algotree.org.
All rights reserved.