Learners will be able to:
- Know how to create a list using Python
- Add data / information to a list
- Manipulate lists using a range of operations
In this lesson you will learn how to use Lists to store, edit and manipulate the data that is stored in them. Adding song lyrics to start a party!
Part 1: Making a List:
Creating a list is very simple, create a variable to store the list and then use [ and ] to complete the list, it is really that simple.
Part 1: Making a List:
Creating a list is very simple, create a variable to store the list and then use [ and ] to complete the list, it is really that simple.
- Look at the code below and run it, how many lists are there?
- Notice that when using strings in a List use, quotations.
- Print out the money list
- Create a new list called "nice things to say", add 5 nice things to say, for example you look nice today.
Part 2: Looping in a List:
You can use loops to loop around a list and then manipulate the contents or use another operation.
You can use loops to loop around a list and then manipulate the contents or use another operation.
- Look at the code below and run it, there are two friends in the list and the loop iterates twice, once for each friends.
- Add the names of your friends to the list on line 1.
- Adapt the code so that it says "Dave is my friend", replace Dave with your friends names.
- Use a similar structure that counts down from ten to zero and then says "Blast Off", 10, 9, 8, 7,6 etc...Blast Off!
Part 3: Print a Particular Entry:
Lists entries are stored in a numbered position, remember that the first position value is zero. You can print a particular position value by using the code print name_of_list[3]. This will print out the entry at the 4th position.
Lists entries are stored in a numbered position, remember that the first position value is zero. You can print a particular position value by using the code print name_of_list[3]. This will print out the entry at the 4th position.
- In the code window below create a new list and add six entries.
- Print out the 3rd and the 5th entries
- Show another students your list and ask them for a word from the list, use the code line to print out that word
Part 4: Adding New Entries to an Existing List:
You can add new entries to a list, so that the list grows! There are two methods append and extend. Look at the code below for a very important list. To add a new word use the code line name_of_list.append("entry")
You can add new entries to a list, so that the list grows! There are two methods append and extend. Look at the code below for a very important list. To add a new word use the code line name_of_list.append("entry")
- Run the code, the party is not started.
- Append the following lines to get the party started, C, A, Young Man, Ways to have a good time
- Instead of using append change it to extend, what happens?
- Can you think of when you may use this?
- Create a program to use the extend function. (Maybe separating a large number?)
- You can also add an entry at a particular point you want using name_of_list.insert(i, x)
- Add the phrase "Stop the Party!!" at the 1st position
- Can you research and find another method?
Part 5: Reverse that List:
Using the code window above reverse the list so that it prints out in reverse order. This uses the code line name_of_list.reverse() then you can print the list.
Part 6: Find the length of a List:
How long is a list? You could count the entries manually but this is a little chewie! So instead use the len() function which will measure and return the length if the list.
Using the code window above reverse the list so that it prints out in reverse order. This uses the code line name_of_list.reverse() then you can print the list.
- Print the list in the original order and then print it it reverse.
- Lists are also mutable which means that you can change or replace the entry, for example replace the Y with a Z.
- Replace the Y above using the code the_YMCA_party_list[0] = Z
- Now replace the M with the phrase "Have a good time"
Part 6: Find the length of a List:
How long is a list? You could count the entries manually but this is a little chewie! So instead use the len() function which will measure and return the length if the list.
- Run the code window below
- What number does it give you?
- Add extra entries to make the total printed 15
- You can also check if a particular value is in a list using print 35 in a_list
- Check if the following values are in the list, 34, 12, 6, 23
Part 7: Try some Other List Tools:
Try some of these other List tools
Further reading here:
Try some of these other List tools
- list.append(x)
- list.remove(x)
- list.pop([i])
- list.sort(cmp=None, key=None, reverse=False)
Further reading here: