From string to list
sentence = "Python's name is derived from the tv series Monty Python."
Words are made up of characters, and so are strings in Python, like the string stored in the variable sentence above. For the sentence above, it might seem more natural for humans to describe it as a series of words, rather than as a series of characters. Say we want to access the first word in our sentence. If we type in:
first_word = sentence print(first_word)
Python only prints the first character of our sentence. If we want the first word, we can transform our sentence into a list of words (represented by strings) using the split() function as follows:
words = sentence.split() print(words)
split() is a function: functions provide small pieces of helpful, ready-made functionality that we can use in our own code. Here, we apply the split() function to the variable sentence and we assign the result of the function (we call this the ‘return value’ of the function) to the new variable words.
By default, the split() function in Python will split strings on the spaces between consecutive words and it will returns a list of words. However, we can pass an argument to split() that specifies explicitly the string we would like to split on. In the code below, we will split a string on commas, instead of spaces.
song_string = "Jerusalema,ikhaya,lami" song_list = song_string.split(",") print(song_list)
Note: when you want to split a word in letters, you don’t use the split() function, but the list() function:
word = 'word' letters = list(word) print(letters)
From list to string
The reverse of the split() function can be accomplished with join(), it turns a list into a string using spaces as a default. You can specify a ‘delimiter’ or the string you want to use to join the items.
song_list = ["Jerusalema", "ikhaya", "lami"] song_string = ' '.join(song_list) print(song_string) delimiter = "," song_string = delimiter.join(song_list) print(song_string)
Exercise: Insert one or more graphical elements in a songline
Finding elements in a list
List variables are very similar to strings. We can for example access its components using indexes and we can use slice indexes to access parts of the list.
Write a small program that defines a variable first_word and assign to it the first word of our wordlist 'words' from above. Do the same for the fifth word, the last word and the last but one word.
Adding elements to a list
list acts like some kind of container where we can store all kinds of information. We can access a list using indexes and slices. We can also add new items to a list. For that you use the function
We first declare an empty list using square brackets. Next, we add some good books to the list.
# start with an empty list good_reads =  good_reads.append("The Hunger games") print(good_reads) good_reads.append("A Clockwork Orange") print(good_reads)
If for some reason we don’t like a particular book anymore, we can change it as follows using the old item’s index.
good_reads = "Pride and Prejudice" print(good_reads)
It is no problem to reset or update an individual item in a list. This is different, however, for strings. With the previous code applied to a string you might try to change a single character in a string. This will raise an error: this is your computer signalling that something is wrong. This is because
strings(and some other types) are immutable. That means that they cannot be changed using the index, as opposed to
lists which are mutable. Strings use the replace() function.
Remove an item from the list
Python provides the function
remove() that you can call on a list and which takes as argument the item we would like to remove.
remove() will only delete the first item in the list that is identical to the argument which you passed to the function.
alphabet = ["ABC", "DEF", "GHI", JKL", "DEF"] print(alphabet) alphabet.remove("DEF") print(alphabet) alphabet.remove("DEF") print(alphabet)
Just as with strings, we can concatenate two lists using the
# first we specify two lists of strings: alphabet_begin = ["ABC", "DEF", "GHI", "JKL"] alphabet_end = ["MNO", "PQR", "STU", "VWX", "YZ"] # then we combine them all_alphabet = alphabet_begin + alphabet_end print(all_alphabet) or alphabet_begin += alphabet_end print(alphabet_begin)
Sorting the elements of a list
We can sort our collection alphabetically with the sort() function. If your collection contains words that start with a capital, they will be sorted first. You can solve this by applying the function lower() to the initial string object.
sorted_alphabet = sorted(all_alphabet) print(sorted_alphabet) or all_alphabet.sort() print(all_alphabet)
What you’ve learned
- mutable versus immutable