We’ve been using a value to assign to a variable. What if we have more values than variables? Say, a variable ‘breakfast’ could be ‘eggs’ but also be ‘ham’? Well, it’s time lists come into play.

What Is a List?

A list is a sequence of values. Each value is an element or an item. Those values don’t need to be the same type. You can even make a list contain another list (nested list).

A list covers its contain within square brackets. If there is no value in the square brackets, then you’ve got an empty list.

>>> breakfast = ['ham', 'eggs', 'cheese']
>>> number = [16, 1608, 2609]
>>> empty = []

Or, more directly,

>>>l = list()

Lists are Mutable

Just like strings, you can index through a list or slice a list into smaller ones.

>>> breakfast[1]
'eggs'
>>> breakfast[:2]
'ham', 'eggs'

But unlike strings, values in a list are not fixed, you can change or modify them as many times as you want.

>>> breakfast[2] = 'orange'
>>> breakfast
'ham', 'eggs', 'orange'

Quick question, what is the output of the following code?

>>> l=[]
>>> l[2]='eggs'

Traversing a List

The most common way to read through a list is by using the for loop.

for stuff in breakfast:
    print(stuff)

Or, to make it in the hard way,

for i in range(len(breakfast)):
    print(breakfast[i])

In case of nested list, the internal list is treated as an item.

breakfast = ['ham', ['eggs','bacon'],'cheese']
for stuff in breakfast:
       print(stuff)

Output

ham
['eggs', 'bacon']
cheese

List Operations

  • adding
>>> a = [1,2,3,4]
>>> b = [5,6,7]
>>> a + b
[1, 2, 3, 4, 5, 6, 7]
  • multiplying
>>> a*3
[1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4]

List Method

You can check how many methods you can use upon lists by using dir(). There are many of them in fact, but I’m going to talk about append() and sort() only, because these two guys are kinda popular.

  • append()
    append() is used to add an item into a list.
strg = 'We wish you a merry Christmas'
lst = list()
strg = strg.split()
for word in strg:
    lst.append(word)
print(lst)

Output

['We', 'wish', 'you', 'a', 'merry', 'Christmas']
  • sort()
    sort() arranges items in list from low to high
>>> lst = ['We', 'wish', 'you', 'a', 'merry', 'Christmas']
>>> lst.sort()
>>> lst
['Christmas', 'We', 'a', 'merry', 'wish', 'you']

Remember that sort() sorts things right there in the list. If you try to print the lst.sort() or assign it to another variable, it will return None. Try it, I’m not lying.

In case you want to sort things from high to low, a reverse=True is the trick.

>>> lst = ['We', 'wish', 'you', 'a', 'merry', 'Christmas']
>>> lst.sort(reverse=True)
>>> lst
['you', 'wish', 'merry', 'a', 'We', 'Christmas']

One more thing to remember, you can only sort items in the same type. It’s kinda understandable. How can you compare ‘A’ and ‘0’ anyway?

List and Strings

I’ve shown you how to split a string into words using split() and add them into a list. How about joining a list of character into a string? See the example:

>>> lst = ['We', 'wish', 'you', 'a', 'merry', 'Christmas'] 
>>> delimiter = ' ' 
>>> strg = delimiter.join(lst)
>>> strg 
'We wish you a merry Christmas' 
>>> type(strg) 
<class 'str'=""></class>

delimiter is to indicating which character to use to separate each word. And join() is like a reverse of split().

More Ways to Play With Lists

  • sum()
>>> l = [1,6,0,8]
>>> sum(l)
15
  • deleting elements
    The simplest way to delete elements from a lists is by using del
>>> lst = [1,6,0,8]
>>> del lst[2]
>>> lst
[1, 6, 8]

If you want to save the deleted elements somewhere, then pop() is for you.

>>> lst = [1,6,0,8]
>>> x = lst.pop(l[2])
>>> lst
[6, 0, 8]
>>> x
1

del also works with a slice of list

>>> lst = [1,6,0,8]
>>> del lst[1:3]
>>> lst
[1, 8]

When you know exactly what element you want to delete but not sure about its index, then remove it.

>>> lst = [1,6,0,8]
>>> lst.remove(1)
>>> lst
[6, 0, 8]

Note that remove returns None, so don’t write lst = lst.remove(1)