This is the source code to follow the examples explained in our YouTube video on Python Lists.
The best way to learn about lists is to follow the video and the example, starting with this source code and experimenting with your changes.
If you need help feel free to contact ShedCoders – remember First session is free for our YouTube subscribers.
from pprint import pprint
# List of integers
numbers = [10, 5, 20, 10, 30]
# List of dictionaries
people = [
{"name": "Alice", "score": 30},
{"name": "Bob", "score": 25},
{"name": "Charlie", "score": 35}
]
#append example
numbers.append(25)
people.append({"name": "David", "score": 28})
#extend example
numbers.extend([35, 40])
people.extend([
{"name": "Eve", "score": 32},
{"name": "Frank", "score": 29}
])
#insert example
numbers.insert(2, 1)
people.insert(2, {"name": "Grace", "score": 27})
#remove example
numbers.remove(10)
people.remove({"name": "Alice", "score": 30})
# Uncomment this when trying it the first time
# last = numbers.pop()
# first = numbers.pop(0)
# print("First:", first)
# print("Last:", last)
# last = people.pop()
# first = people.pop(0)
# print("First:", first)
# print("Last:", last)
#sort example
numbers.sort()
people.sort(key=lambda x: x["score"], reverse=True)
## Printout of data
print()
print("Numbers:", numbers)
print("*" * 40)
pprint(people)
print()
