Lists in Python contain all sorts of different data types, for example, a perfectly valid list could be:

>>> random_stuff = [ 3, 'x', [ 'tyre', 'steering wheel' ], True, 4.7 ]
>>> print(random_stuff)
[3, 'x', ['tyre', 'steering wheel'], True, 4.7]

The above contains an integer, a float, a string, a boolean True and even another list.

Lists are also mutable, meaning that they can be changed after they have been defined, and there is a bunch of handy methods to do just that.

Instead of the messy list defined above, I’m going to use an easier one containing some artists I like:

artists = [ 'Nina Simone', 'She Drew the Gun', 'Knitting for Beginners', 'Faithless' ]

Append – Adding an Item to the end of a List

Append is easy, and it does one thing – adds an element to the end of the list, for example:

>>> artists.append('Porcupine Tree')
>>> print(artists)
['Nina Simone', 'She Drew the Gun', 'Knitting for Beginners', 'Faithless', 'Porcupine Tree']

Simple. But what if I wanted to put an artist in the middle of the list?

Insert – insert an Item anywhere in the list by Index

Insert can be used to put a new item anywhere in the list by its index. In the code below, I’m adding George Michael third in the list using index number 2 (keeping in mind that the index for the list starts at zero):

>>> artists.insert(2, 'George Michael')
>>> print(artists)
['Nina Simone', 'She Drew the Gun', 'George Michael', 'Knitting for Beginners', 'Faithless', 'Porcupine Tree']

Pop – Remove List Items

Woah, my list is getting long. I’m going to remove someone – calling the pop method without any parameters will remove the last item from the list:

>>> artists.pop()
'Porcupine Tree'
>>> print(artists)
['Nina Simone', 'She Drew the Gun', 'George Michael', 'Knitting for Beginners', 'Faithless']

I can also use pop to remove an element by its index – here I’m removing George Michael by index 2:

>>> artists.pop(2)
'George Michael'
>>> print(artists)
['Nina Simone', 'She Drew the Gun', 'Knitting for Beginners', 'Faithless']

If I wanted to remove the first list element, I’d use the index zero, eg artists.pop(0).

Remove method – remove element by value

If you don’t know the index of an element but you do know the value, you can still remove it:

>>> artists.remove('Faithless')
>>> print(artists)
['Nina Simone', 'She Drew the Gun', 'Knitting for Beginners']

This will only remove the first instance of the value – if there’s more than one instance of the value and you want to remove them all you’d have to loop through the list.

Del command

The del command is useful because not only can it delete single list items, but it can delete a range of items. First I’ve reset my list so we have some more items to delete:

artists = [ 'Nina Simone', 'She Drew the Gun', 'Knitting for Beginners', 'Faithless' ]

To delete just one item, you can do it by index:

>>> del artists[-1]
>>> print(artists)
['Nina Simone', 'She Drew the Gun', 'Knitting for Beginners']

Here I’m using negative indexing – which starts from the end of the list at -1 and counts negatively backwards – to remove the last item.

I can also remove a range of items using the del command. I’m using string slicing to remove everything from the index of the first item I want to remove up until the index of the last item I want to remove, plus one. I’m going to remove She Drew the Gun (index 1) and Knitting for Beginners (index 2 +1):

>>> artists = [ 'Nina Simone', 'She Drew the Gun', 'Knitting for Beginners', 'Faithless' ]
>>> del artists[1:3]
>>> print(artists)
['Nina Simone', 'Faithless']

Clear method – delete the whole list contents

The clear method is simple, it clears the list entirely:

>>> artists.clear()
>>> print(artists)
[]

Replace value of list item by index

Finally, I can overwrite the value of a list item with a new value using its index and the assignment operator:

>>> artists = [ 'Nina Simone', 'She Drew the Gun', 'Knitting for Beginners', 'Faithless' ]
>>> artists[1] = 'Moloko'
>>> print(artists)
['Nina Simone', 'Moloko', 'Knitting for Beginners', 'Faithless']

Adding and Removing Lists Items in Python