Tutorials‎ > ‎

Week 2(b): More on Lists, Strings and Sets

Below I described some of the many ways that we can use and manipulate lists, strings and sets in the Python language. Once you've read this page, you should be able to find a way to solve Mini-Challenge #2 with a single line of Python code.


We have already come across several ways to create new lists. For example,

[]          # create an empty list
[1] * 5     # create the list [1, 1, 1, 1, 1]
range(5)    # create a list of numbers [0, 1, 2, 3, 4]
[i for i in range(10) if i % 2 == 0]  # create a list of even numbers [0, 2, 4, 6, 8]
range(10)[::2]  # another way to get the same list of even numbers

We have also seen that we may access the elements in a list by referring to them by their list index. Remember, the first element has list index 0, the second has index 1, and so on. Try

a = range(10)  # create a list of numbers [0, 1, ..., 9] and assign to variable a
print a[2]     # print the third element of the list, i.e. 2
a[2] = 100     # change the value of the 3rd list element
a              # display the modified list

If we use an index which is greater than or equal to the length of the list, an error arises. To find the length of a list, use the len function. The last element in list a will have an index of len(a) - 1.

We can create a new lists by slicing existing lists. For example, try

a = range(10)  # assign the list [0,1, ..., 9] to variable a
a[2:9]         # get the sublist [a[2], ... , a[8]]
a[2:9:3]       # every third element of the sublist, i.e. [a[2], a[5], a[8]]

We can also refer to the last element of a list using the index -1, i.e. a[-1]. The penultimate element is a[-2], and so on. This makes it straightforward to make a new list which is the reverse of an existing list:
[a[-1-i] for i in range(len(a))]

There is also a function to reverse the list in-place. Try for example:

a = range(10)
a   # display the reversed list

To get a new list sorted in ascending order, we can use:


Alternatively, to sort a list in-place, we can use the sort() function of the list class:

a = [4,2,3,1]

We have already seen how to add elements to a list, using append. There are some other really useful functions in the list class. Try entering the following sequence of commands,

a = [1]         # make a new list with one element, 1
a.append(3)     # add an element at the end, so a = [1,3]
a.insert(1, 2)  # insert an element 2 at index position 1, so a = [1,2,3]
a.extend([4,7]) # add a new list onto the end, so now a = [1,2,3,4,7]
a.remove(7)     # remove the element with value 7 from the list
a               # report the values in the list

Finally, we can check to see whether two lists are equal, using the == operator. Two lists are equal if and only if they contain precisely the same elements in the same order

[1,2,3] == [1,2,4]  # Returns False
[1,2,3] == [1,3,2]  # Returns False
[1,2,3] == [1,2,3]  # Returns True

For yet more on lists, see the Python documentation.


A string is simply a list of characters in order. A character is anything you can type on the keyboard in one keystroke, like a letter, a number, or a backslash. For example, "hello" is a string.

Python handles strings in a similar way to lists, and we can use many of the same commands:

a = "hello everyone!" # assign a new string to variable a
len(a)          # the number of characters in a (including space and exclamation mark)
a[4]            # the fifth character in the string
a.count("e")    # the number of times the letter e appears
a[6:]           # the string from the 7th character onwards, i.e. the second word
a[::2]          # every other character in the string

We can turn a number into a string using the str function, e.g.

str(7+5)  # is the same as '12'

Strings can be concatenated (joined together), using the + operator:

"hello" + " everyone!"

It's easy to search through a string for a keyword or phrase. Let's look for "ev" inside "hello everyone!":

a = "hello everyone!"
b = "ev"
b in a     # True if "ev" is contained within "hello everyone!", False otherwise
a.find(b)  # Returns the index of the start of the substring b within a.

It's easy to reverse a string, using the slicing notation:


It's simple to convert a string into a list of characters, and vice versa:



A new set is defined in a similar way to a list, but we use the curly brackets { and }. For example,


A set is like a list, but with two crucial differences. Firstly, the elements in a set are not ordered. So {1,3,2} is the same set as {1,2,3}. Secondly, an element can only appear once in a set. Duplicate elements are dropped. So {1,2,3,3} is the same set as {1,2,3}.

Let's check this:

{1,2,3,3} == {1,2,3}  # Returns True
{1,3,2} == {1,2,3}  # Returns True

Contrast this with the behaviour of lists: recall that two lists are equal if and only if they have the same elements in the same order.

In Mini-Challenge 2, we need to find the number of different digits featuring in a large number. One way to do this is: (i) convert the number to a string using str, (ii) convert the string to a set, using set, to eliminate duplicates, (iii) find the number of elements in the set, using len.  For example,

a = 9125146422