Python Lists

Features of a List
A list is a collection of objects. It contains the following features:
- It is ordered
- It is changeable
- It allows duplicate members
Create a list in Python
Access an item or items in a list
Lists are start at position 0. You can use slice notation like we covered in the strings chapter
Loop through a Python List
You can use a for loop to iterate through a list.
Check if an item exists in a list
Use the “in” keyword to check if an item exists in a list.
Get the length of a Python List.
Use the len() method to get the length of a list
Sort a Python List
Use the sort() method to sort a list.
Reverse sort items in a Python List
Use the reverse() method to reverse sort a list.
Add a single item to a Python List
Add a single item to a Python list using the append() method.
Add multiple items to a Python List
Add multiple items to a Python List using the extends() method.
Add an item to a Python List at a specific Location
To add an item to a list at a specific location use the insert() method.
Remove an item from a Python List by index using pop()
To remove an item from a Python List by index use the pop() method.
Remove an item from a Python List by index using del keyword
To remove an item from a Python List by index use the del keyword.
Remove an item from a Python List by value
To remove an item from a Python List by value use the remove() method.
Remove all items from a Python List (empty a list)
To remove all items from a Python List use the clear() method.