Getting Things From Lists¶
One cool thing about lists is that you can grab certain items from the list by using slices or indicies. You don’t really need to remember those terms, but there are two things you should remember:
- The first item in the list isn’t
- We get the item in the list that we want using
>>> [1, 2, 3, 4] 1
This way looks a little goofy and confusing, so let’s assign the list to a variable.
>>> favorite_numbers = [1, 2, 3, 4] >>> favorite_numbers 1
We can also get a portion of things from a list:
>>> favorite_numbers = [1, 2, 3, 4] >>> favorite_numbers[0:2] [1, 2]
Wait a second. We’ve asked for the items in the list from 0 to 2. The value at index 0 is 1, the value at index 1 is 2, and the value at index 2 is 3. So why did we only get
[1, 2] back?
This is because the
slice sytax (the way we get just a portion of items from a list) returns the first item in the slice up to, but not including the second value. Tricky.
What about getting things a single item from a list within a list? Remember our list of things and PyCon feelings?
>>> more_things = [things, pycon_feelings] >>> more_things [[5, True, 'hello'], ['Hooray', 'Good', 'Amazing', 'Lacking bees']]
How do we get
True from the list
more_things? Well, what does this give us?
It gives us the list
things, since that’s what’s at index 0. Can we build on
more_things to get the second item in in _that_ list?
>>> more_things True
One last topic on lists: changing them.
You can add to a list like this:
>>> my_list = ["lions", "tigers", "bears"] >>> my_list ['lions', 'tigers', 'bears'] >>> my_list.append("Oh my") >>> my_list ['lions', 'tigers', 'bears', 'Oh my']
You can remove items a couple of ways.
You can remove a specific item by its value:
>>> my_list.remove("lions") >>> my_list ['tigers', 'bears', 'Oh my']
You can remove and return the last item in the list using
>>> my_list.pop() 'Oh my' >>> my_list ['tigers', 'bears']
And you can remove and return an item by its index:
>>> my_list.pop(0) 'tigers' >>> my_list ['bears']
Your Turn: More Lists 🏁¶
More List Exercises¶
Random Vacation Planner¶
Create a program that plans five random vacations by:
- Asking for input for five locations
- Choose a random location and random duration
- Print the five destinations and number of days of the trip for each.
No destination should appear in more than one trip.
State Capital Guesser¶
Copy the contents of
states_and_capitals.py into a new file
capital_guesser.py. In this new file, create a program that does the following.
- Chooses a state to quiz at random
- Asks the user for a guess of the capital city of that state
- Prints “Yay! Good job!” if the answer is correct, and “Sorry, that’s incorrect.” if not.
$ python3.6 capital_guesser.py What is the capital of Montana? Helena Yay! Great job! $ python3.6 capital_guesser.py What is the capital of Georgia? Sacramento Sorry, that's incorrect.
Better Lazy Chef¶
Expand your lazy chef program to do the following:
- Choose a name at random from a name of sandwiches and assign your random sandwich ingredients to that name. Your menu should not include two sandwiches with the same name.
- Print out your menu items in a nice, human-readable way.
If you didn’t make a lazy chef program previously, you can use these lists to start with:
proteins = ["ham", "turkey", "beef", "tofu"] condiments = ["mustard", "mayo", "hummus"] veggies = ["tomato", "lettuce", "onion", "sprouts"] breads = ["white bread", "wheat bread", "pita bread", "sourdough"]
$ python3.6 lazy_chef.py The Terminator has turkey, lettuce, and mayo on rye bread. The First Lady has tofu, tomato, and hummus on pita bread. The San Diegan has tuna, sprouts, and dill spread on sourdough.
Acquiring Oranges’s First and Last Assets¶
- Make a list
my_assetscontaining your assets, and a list
orange_assetscontaining Orange, Inc.’s assets
- Make a new list
my_orange_assetsthat contains all of your assets, and Orange’s first and last assets. Hopefully those are their best products!