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:

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].

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].

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

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

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.

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.

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

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

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

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

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

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’.

…and swap the 9 for an 11:

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

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.

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

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

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

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.

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

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.

Python | Finance

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store