A Journey through Multi-dimensional lists
Continuing from our previous discussion on lists, it's now time to talk about multi-dimensional lists or what we also refer to as jagged arrays in other languages.
But first, let's talk about list comprehensions.
List comprehensions are almost considered magic in Python. They provide an easy way to make a list but really shine when performing some programming operations which may require more than a few lines in other programming languages.
Imagine you wanted to retrieve the first 10 algebraic numbers nicely formatted as a list. We will also make use of the
range() function here as a bonus.
[x for x in range(10)] # returns [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Nice, one liner aka the Pythonic way. But, but, but it starts with 0 and ends with 9 i hear you say!
Remember list indexing starts at 0 so it makes sense to increase our index by 1.
Alright, let's get this in action:
[x+1 for x in range(10)] # returns [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Aha, now we get the desired effect.
Imagine you wanted to calculate the square root of the first 10 numbers and return the result as a list.
[(x+1) ** 2 for x in range(10)] # returns [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
The Multi-dimensional list explained
Now if we wanted to created a multi-dimensional list having any dimension matrix, we can use our knowledge of list comprehensions to good effect.
Let's create a
3D multi-dimensional list.
multi_list = [[ for i in range(3)] for i in range(3)] # [[, , ], [, , ], [, , ]]
Just replace the number 3 to any other number to alter the dimensional matrix.
multi_list_5d = [[ for i in range(5)] for i in range(5)] # [[, , , , ], [, , , , ], [, , , , ], [, , , , ], [, , , , ]]
Note how we have used a list comprehension within another list comprehension to get the ability to create any dimensional list.
We can now manipulate our list to add/remove items at will using indexing.
multi_list = 'A' # [[, 'A', ], [, , ], [, , ]] multi_list = 'X' # [[, 'A', ], [, , ], [, , 'X']]
Let's now talk about other useful methods available to lists.
Finding the position of an existing item in a list
It is easy to find the position of an existing item within a list using the
list.index() method. Let's see how. If the item does not exist, a
ValueError is raised.
fruits = ['apples', 'bananas', 'oranges', 'grapes'] fruits.index('grapes') # returns 3 fruits.index('a') # returns ValueError: 'a' is not in list
Adding an item anywhere in a list.
As we did earlier, we can use the index position to insert an item in a list using the
This method uses the first argument as index, and the value being inserted as the second argument.
Let's see an example:
sports = ['football', 'basketball', 'tennis'] sports.insert(0, 'volleyball') # returns ['volleyball', 'football', 'basketball', 'tennis']
Removing the last item from a list.
One can use the
list.pop() to remove and return the last element from the list. This method requires no argument by default but can also accept as first argument the index position of an item to be removed.
Let's see how this is implemented:
students = ['Angela', 'Toby', 'Reginald', 'Archie'] students.pop() # removes and returns 'Archie', students now ['Angela', 'Toby', 'Reginald'] students.pop(1) # removes and returns 'Toby', students now ['Angela', 'Reginald']
Clearing all items from a list
This demonstrates how we can remove all items from a specific list using the
list.clear()method. Please note this works in Python 3 only.
students = ['Angela', 'Toby', 'Reginald', 'Archie'] students.clear() # students is now , clean as a whistle.
In Python 2, we can make use of the slice operator [:] as we have seen in part 1 of this series.
students = ['Angela', 'Toby', 'Reginald', 'Archie'] students[:] =  # students is now , clean as a whistle.
Convert a string to a list.
There are times when you would want to break a string of characters and convert each character into an item in a list. Let's see how to do that.
hello = list('hello world') # hello is now ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
Do you use lists in Python or want to suggest an improvement to this article? Please share it with us by leaving a reply below.
Stay tuned for Part 3.