3.5. Converting To Lists#
It is often useful to convert variables in Python to a list so you can perform
list operations. This can be achieved using list().
list(iterable)
Only iterables can be converted to a list. An iterable is a sequence of values you can go through.
Example: Convert a string to a list. This will create a list of characters that make up the given string.
text = 'Hello World!'
print(list(text))
['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd', '!']
If we want to create a list of strings rather than characters we can use the
.split() method.
The string is the text that you want to convert to a list and the separator is used to identify how the string should be broken up into different elements.
Example: Splitting on a space.
text = 'Red Orange Green Blue'
print(text.split(' '))
['Red', 'Orange', 'Green', 'Blue']
The separator you use doesn’t have to be a character, it can be a string.
Example: Splitting on a comma and a space.
weekdays = 'Monday, Tuesday, Wednesday, Thursday, Friday'
print(weekdays.split(', '))
['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday']
Question 1
What do you think the output of the following code will be?
numbers = '12345678910'
print(list(numbers))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 0]
['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
['1', '2', '3', '4', '5', '6', '7', '8', '9', '1', '0']
Solution
Python will treat numbers as a string. This means when it is convert to a list using list() it will create a list of characters. This means:
The elements of the new list will be characters, not integers
The last number, which is 10 will not be treated as a single number it will be broken up into the characters ‘1’ and ‘0’.
Question 2
What do you think the output of the following code will be?
fruits = 'apples,bananas,pears'
list_fruits = fruits.split(',')
print(list_fruits[1])
a
p
,
apples
bananas
pears
Solution
Solution is locked
Code challenge: String to List
Suppose you had the following string.
months = 'Jan-Feb-Mar-Apr-May-Jun-Jul-Aug-Sep-Oct-Nov-Dec'
Write a program to convert this string into a list and prints the result. Your output should look like this:
['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
Solution
Solution is locked
Code challenge: Shopping List
Write a program that asks the user what they need to buy from the shops and converts this into a list of items. You can assume the user separates each item with a space.
Example 1
What do you need to buy? spaghetti tomatoes basil burrata
['spaghetti', 'tomatoes', 'basil', 'burrata']
Example 2
What do you need to buy? flour eggs milk sugar
['flour', 'eggs', 'milk', 'sugar']
Solution
Solution is locked
Code challenge: Secret Message
You want to send your friend a secret message. A simple way to do this is to put a jumble of characters between each letter you want to send. For example, if you want to send the message hello you might scramble this by placing the string "qmcsyd" between each letter. This means your secret message would look like this:
hqmcsydeqmcsydlqmcsydlqmcsydo
Write a program to decode this secret message. Your program should ask for the encrypted message and also for the string they used to scramble the message. You program should then return the decoded message.
Example 1
secret message: hqmcsydeqmcsydlqmcsydlqmcsydo
scramble string: qmcsyd
hello
Example 2
secret message: tkiblosekhkiblosekekiblosekrkiblosekekiblosek kiblosekakiblosekrkiblosekekiblosek kiblosekckiblosekokiblosekokiblosekkkiblosekikiblosekekiblosekskiblosek kiblosekikibloseknkiblosek kiblosektkiblosekhkiblosekekiblosek kiblosekckiblosekukiblosekpkiblosekbkiblosekokiblosekakiblosekrkiblosekd
scramble string: kiblosek
there are cookies in the cupboard
Solution
Solution is locked