End of Summer - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
List basics
Lists with if statements and loops
Lists in functions
Summary
22. Summary

Instruction

All right, it's time to wrap things up!

• List definitions require elements separated with commas inside square brackets:

invited_guests = ['Mark', 'Hannah', 'Walter']
• List elements are accessed with square brackets. Indexing starts at 0.

# first element
invited_guests[0]
# first and second element
invited_guests[0:2]

• To add a new element:

# way one
invited_guests.append('New Guest')
# way two
invited_guests + ['New Guest']

• To delete an element from a list:

# when you know the index
del invited_guests[2]
# when you know the value
invited_guests.remove('Hannah')

• To iterate over a list:

# when you don't need the index
for guest in invited_guests:
print(guest)
# when you need the index
for guest in range (0, len(invited_guests)):

• Lists can be used as function arguments. They can also be returned from functions.

Alright, it's time for a short quiz!

Exercise

Click to continue.