After reading lesson 26, you’ll be able to
- Build lists whose elements are lists
- Sort and reverse list elements
- Convert a string into a list by splitting on a character
A list is typically used to represent a collection of items, frequently but not necessarily of the same type. You’ll see that it may be useful for the list elements to be lists themselves. For example, suppose you want to keep a list of all the items in your house. Because you have many items, it’ll be more organized to have sublists, where each sublist represents a room, and a sublist’s elements are all the items in that room.
At this point, it’s important to take a step back and understand what has been going on with this new mutable object, a list. Lists are directly modified by any actions you do on them. Because the list is directly modified, you don’t reassign the list to a new variable after an operation; the list itself now contains the changed values. To see the value of the modified list, you can print it.
Consider this
Your friend can recite the number pi up to 100 digits. You add each digit into a list as he tells it to you. You want to figure out how many zeros are in the first 100 digits. How can you quickly do this?
Answer:
If you sort the list, you can count the zeros at the beginning of the list.