Video version:
We need to list things: The users currently visiting our website, the options of a setting or the days of the week. For listing that information we use…lists.
A list is a list of elements, with some methods that help us to perform operations on, and or with that list. Think of adding elements, removing elements, counting them, extracting the max number of a list, etc.
Let’s see an example of a list:
my_list = ["Hey", "Ho", "Let's", "Go"]
That’s a list. A group of elements inside brackets, separated with a comma. As simple as that.
To see how lists works and what we can do with them, let’s try some of its methods:
Append
Append adds a new element to the end of the list. Let’s see:
week = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"] week.append('Sunday') print(week) > ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
Insert
Insert adds an element, but instead of the end of the list, at the position you indicate. Indicate first the index, then the element:
week2 = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Sunday"] week2.insert(5, "Saturday") print(week2) > ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
Pop
Pop removes the last element in a list:
week3 = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] week3.pop() print(week3) > ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
Remove
Remove the element inside the list with the exact text (or number) as passed to the method:
week4 = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] week4.remove("Friday") print(week4) > ["Monday", "Tuesday", "Wednesday", "Thursday", "Saturday", "Sunday"]
Count
Counts how many elements are inside the list:
week6 = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] print(len(week6)) > 7
Those are the main methods used, but there are many more list methods (such as max(), sum(), reverse()…).
I can teach you about each single one, but it will be tedious for you (and one skill you should learn is how to search information). I’ll leave you a link at the end of the post listing every method lists have, but now it’s…
Challenge Time
Today you have 3 challenges. Don’t worry, they’re small and you can solve them with less than 4 lines. Of course, feel free to go back and revisit the first part of this lesson. Our 4 challenges are:
# Exercise: This list is wrong, remove the thing you don't like things_i_like = ['Dogs', 'Cats', 'Programming', 'Breaking a leg', 'Ice cream'] # Exercise: Pop the Wu Tang Clan CD from the shopping list using pop, getting the index first (numerical) my_shopping_list = ['Oranges', 'Rice', 'Wu tang clan CD', 'Wine', 'Cereal', 'Bread'] # Exercise: Add your favourite language at the start of the list languages = ['Java', 'C++', 'Javascript']
As you can see, the exercises are easy. Feel free to try them on your own.
Compare your solution with mine at: https://repl.it/@DavidMM1707/03-Lists
Video version: https://www.youtube.com/watch?v=IKtAHzv7DBw
Find me on Twitter at: https://twitter.com/DavidMM1707
My GitHub: https://github.com/david1707
Learn more about:
– Lists (only the 5.1 point): https://docs.python.org/3/tutorial/datastructures.html
Fourth lesson: 04 – For and While loops