Assessment: Methods: Lists and Strings >> Python Basics
seqmut-1-2: Which method would you use to figure out the position of an item in a list?
✔️ Yes, index will return the position of the first occurance of an item.
Score: 1.0 / 1
seqmut-1-3: Which method is best to use when adding an item to the end of a list?
✔️ Yes, though you can use insert to do the same thing, you don’t need to provide the position.
Score: 1.0 / 1
Write code to add ‘horseback riding’ to the third position (i.e., right before volleyball) in the list sports
.
sports = ['cricket', 'football', 'volleyball', 'baseball', 'softball', 'track and field', 'curling', 'ping pong', 'hockey']
sports.insert(2,'horseback riding')
Expand DifferencesExpand Differences
Result | Actual Value | Expected Value | Notes |
---|---|---|---|
Pass | [‘cri…key’] | [‘cri…key’] | Testing that sports is set correctly. |
Pass | ‘.insert(‘ | “sport…ing’)” | Testing that insert was used in your code. |
You passed: 100.0% of the tests
Score: 1.0 / 1
Write code to take ‘London’ out of the list trav_dest
.
trav_dest = ['Beirut', 'Milan', 'Pittsburgh', 'Buenos Aires', 'Nairobi', 'Kathmandu', 'Osaka', 'London', 'Melbourne']
trav_dest.remove('London')
Expand Differences
Result | Actual Value | Expected Value | Notes |
---|---|---|---|
Pass | [‘Bei…rne’] | [‘Bei…rne’] | Testing that trav_dest is set correctly. |
Pass | True | True | Testing that a method invocation was used in your code. |
You passed: 100.0% of the tests
Score: 1.0 / 1
Write code to add ‘Guadalajara’ to the end of the list trav_dest
using a list method.
trav_dest = ['Beirut', 'Milan', 'Pittsburgh', 'Buenos Aires', 'Nairobi', 'Kathmandu', 'Osaka', 'Melbourne']
trav_dest.append("Guadalajara")
Expand DifferencesExpand DifferencesExpand Differences
Result | Actual Value | Expected Value | Notes |
---|---|---|---|
Pass | [‘Bei…ara’] | [‘Bei…ara’] | Testing that trav_dest is set correctly. |
Pass | ‘+’ | ‘trav_…ara”)’ | Testing that you are not using concatenation (+). |
Pass | ‘.’ | ‘trav_…ara”)’ | Testing that a method invocation was used in your code. |
You passed: 100.0% of the tests
Score: 1.0 / 1
Write code to rearrange the strings in the list
winners
so that they are in alphabetical order from A to Z.
winners = ['Kazuo Ishiguro', 'Rainer Weiss', 'Youyou Tu', 'Malala Yousafzai', 'Alice Munro', 'Alvin E. Roth']
winners.sort()
Expand Differences
Result | Actual Value | Expected Value | Notes |
---|---|---|---|
Pass | [‘Ali… Tu’] | [‘Ali… Tu’] | Testing that winners is set correctly (Don’t worry about actual and expected values). |
You passed: 100.0% of the tests
Score: 1.0 / 1
Write code to switch the order of the
winners
list so that it is now Z to A. Assign this list to the variable z_winners
.
winners = ['Alice Munro', 'Alvin E. Roth', 'Kazuo Ishiguro', 'Malala Yousafzai', 'Rainer Weiss', 'Youyou Tu']
winners.sort(reverse=True)
z_winners = winners
Expand Differences
Result | Actual Value | Expected Value | Notes |
---|---|---|---|
Pass | [‘You…nro’] | [‘You…nro’] | Testing that z_winners is set correctly (Don’t worry about actual and expected values). |
You passed: 100.0% of the tests