Lists In Python

Thomas
4 min readDec 19, 2020

Lists are argubly the most versatile, common and most frequently used data types in Python.

Lists are expressed inside square brackets [] and have the following characteristics:

  • Ordered (maintained structure from inception)
  • Can contain objects and even functions/classes
  • Are mutable
  • Can extract specific indexes

Basic list example:

NumsInTheHat = [2, 91, 34, 56, 32, 5, 65, 3]

We can grab specific indexes from a list using square brackets as well. In Python indexes begin at 0, so the second item in a list will equate to an index of [1].

Num2 = NumsInTheHat[1]
print(Num2)
--------------------------------------------------------------------91

If the list is exhaustively long and it’s easier to do so, we can index a number from the tail end of the list. If we would like to print 3, we can use [-1].

FinalNum = NumsInTheHat[-1]
print(FinalNum)
--------------------------------------------------------------------3

There is more to indexing. We can also specify a range of items . Let’s grab the first 4 numbers of the list.

First4 = NumsInTheHat[0:4]
print(First4)
--------------------------------------------------------------------[2, 91, 34, 56]

In this example, instead of using ‘0’ we can actually leave this blank and lead with the colon. The result will be the same.

First4 = NumsInTheHat[:4]
print(First4)
--------------------------------------------------------------------[2, 91, 34, 56]

There is a 3rd number we can introduce when indexing. This may not be as frequently used but it’s good to know that it exists. It refers to a step function where you can specify the leaps you wish to take, i.e. 2 will output every other number in the list. The example below will make this clear where we wish to print alternate numbers from start to finish.

Alternate = NumsInTheHat[0:7:2]
print(Alternate)
--------------------------------------------------------------------[2, 34, 32, 65]

As previously mentioned, if we wish to access alternate numbers within the entire list, the same result can be generated with leaving the initial 2 numbers blank.

Alternate = NumsInTheHat[::2]
print(Alternate)
--------------------------------------------------------------------[2, 34, 32, 65]

We can also use ‘in’ and ‘not in’ operators to return a boolean (True/False).

NumsInTheHat = [2, 91, 34, 56, 32, 5, 65, 3]Search = 2 and 5 in NumsInTheHat
SearchB = 1 in NumsInTheHat
SearchC = 100 not in NumsInTheHat
print(Search)
print(SearchB)
print(SearchC)
--------------------------------------------------------------------True
False
True

These operators can obviously be useful in ‘if’ statements, for example:

NumsInTheHat = [2, 91, 34, 56, 32, 5, 65, 3]

if 1 in NumsInTheHat:
print('Includes One')
else:
print("Doesn't Include One")

As you might guess, we can easily add (concatenate) values to a list using the (+) operator.

Nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Nums = Nums + [11]
print(Nums)
--------------------------------------------------------------------[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

We can use ‘append’ to achieve a similar result:

Nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Nums.append(11)
print(Nums)
--------------------------------------------------------------------[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]

Viewing the length, maximum (alphabet/numbers) and minimum values is simple within lists as well. If you are familiar with lambda functions the example below shows how we can perform tasks on the list (printing even numbers only and doubling all values).

Nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

print(len(Nums))
print(min(Nums))
print(max(Nums))

print(list(filter(lambda x: x % 2 == 0, Nums))) # EVEN NUMS ONLY
print(list(map(lambda x: x*2, Nums))) # DOUBLE ALL NUMS
--------------------------------------------------------------------10
1
10
[2, 4, 6, 8, 10]
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

As you have now learnt, lists are very versatile. They are deemed ‘mutable’ meaning that can be broken down into component parts, added, moved and deleted. Using the ‘Nums’ list above, if we wish to make some changes the index can be used for this. Let’s change the ‘3’ value and switch it with a ‘1’.

Nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Nums[2] = 1
print(Nums)
--------------------------------------------------------------------[1, 2, 1, 4, 5, 6, 7, 8, 9, 10]

…and swap the 9 for an 11:

Nums[2] = 1Nums[-2] = 11
print(Nums)
--------------------------------------------------------------------[1, 2, 1, 4, 5, 6, 7, 8, 11, 10]

Let’s now remove the ‘11’ that we just changed using the ‘del’ method.

del Nums[-2]
print(Nums)
--------------------------------------------------------------------[1, 2, 1, 4, 5, 6, 7, 8, 10]

A similar result can be achieved with the pop method. We don’t specify a value, we specify the index. Leaving the paretheses blank will simply slice the last value of the list off.

Nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Nums.pop() #REMOVES TRAILING VALUE
print(Nums)
--------------------------------------------------------------------[1, 2, 3, 4, 5, 6, 7, 8, 9]

Let’s ‘pop’ the number 7 off as well.

Nums.pop(6)
print(Nums)
--------------------------------------------------------------------[1, 2, 3, 4, 5, 6, 8, 9]

The insert method can also be used to add items in at a given index. The method takes in the index and the value.

Nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Nums.insert(0, 100)
print(Nums)
--------------------------------------------------------------------[100, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

If we wish to change an entire block of the list, we can also specify a range to save time:

Nums[3:5] = [11, 12, 13]
print(Nums)
--------------------------------------------------------------------[1, 2, 3, 11, 12, 13, 6, 7, 8, 9, 10]

Note that we don’t need the range to match the new batch of the list. For example let’s change ‘4’ with 5 random numbers in the original Nums list.

                HERE
|

Nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Nums[3:4] = [19, 21, 23, 98, 101]
print(Nums)
--------------------------------------------------------------------[1, 2, 3, 19, 21, 23, 98, 101, 5, 6, 7, 8, 9, 10]

….or repeat the above but keep the ‘4’ in the list. Specify the index range with the same number.

Nums[3:3] = [19, 21, 23, 98, 101]
print(Nums)
--------------------------------------------------------------------[1, 2, 3, 19, 21, 23, 98, 101, 4, 5, 6, 7, 8, 9, 10]
|
REMAINS

Conclusion

As you can see, lists are incredibly dynamic. You can modify them in any which way using a variety of methods.

If you are new to Python, the best way to learn is to play around with lists similar to this tutorial in order to gain an understanding.

--

--