- A couple more tricks can be performed with lists to make them really useful
#List containing 4 names, and print
names = ["Joe","Mike","Sarah","Charlie"]
print(names)
#Sort and reprint the list Ascending (A-Z)
names.sort()
print(names)
#Sort and reprint the list Descending (Z-A)
names.sort(reverse=True)
print(names)
#A way to search a specific record and get the index
#without using for loop. This is no good for lists
#containing duplicate values
index = names.index("Mike")
print(index)
Tasks
- Type in the code and make sure it works
- Create a new list with 10 numbers in it, and print them sorted into order
- Modify the program to accept user input to add more items to the list
- Save, print and label your code
Extension Task
When you have a list with duplicate values, the quick search technique above will not work. When faced with duplicate values, you must use a FOR loop to cycle through the list and check each item individually, and count, or display the occurrences. Can you do this? Refer back to the earlier list tasks to help.