Python Lesson 11: Lists in Python

  1. Create a list in Python
  2. Access an item or items in a list
  3. Loop through a Python List
  4. Check if an item exists in a list
  5. Get the length of a Python List.
  6. Sort a Python List
  7. Reverse sort items in a Python List
  8. Add a single item to a Python List
  9. Add multiple items to a Python List
  10. Add an item to a Python List at a specific Location
  11. Remove an item from a Python List by index using pop()
  12. Remove an item from a Python List by index using del keyword
  13. Remove an item from a Python List by value
  14. Remove all items from a Python List (empty a list)

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

#Lists are written with square brackets
shoppinglist = ["milk","bread","apples","eggs","rice"]
print(shoppinglist)
Notebook Output

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

#Create a list
shoppinglist = ["milk","bread","apples","eggs","rice"]
#print the first item in the list
print(shoppinglist[0])
#print the last item in the list
print(shoppinglist[-1])
#print items starting at position 2 and ending before position 4
print(shoppinglist[2:4])
Notebook Output

Loop through a Python List

You can use a for loop to iterate through a list.

shoppinglist = ["milk","bread","apples","eggs","rice"]
for x in shoppinglist:
    print(x)
Notebook Output

Check if an item exists in a list

Use the “in” keyword to check if an item exists in a list.

shoppinglist = ["milk","bread","apples","eggs","rice"]
if "milk" in shoppinglist:
    print("milk is in your shopping list")
Notebook Output

Get the length of a Python List.

Use the len() method to get the length of a list

shoppinglist = ["milk","bread","apples","eggs","rice"]
print(len(shoppinglist)
Notebook Output

Sort a Python List

Use the sort() method to sort a list.

shoppinglist = ["milk","bread","apples","eggs","rice"]
shoppinglist.sort()
print(shoppinglist)
Notebook Output

Reverse sort items in a Python List

Use the reverse() method to reverse sort a list.

shoppinglist = ["milk","bread","apples","eggs","rice"]
shoppinglist.reverse()
print(shoppinglist)
Notebook Output

Add a single item to a Python List

Add a single item to a Python list using the append() method.

shoppinglist = ["milk","bread","apples","eggs","rice"]
#add cookies to the list using append
shoppinglist.append("cookies")
print(shoppinglist)
Notebook Output

Add multiple items to a Python List

Add multiple items to a Python List using the extends() method.

#Use extend to add a collection to a list
proteinlist = ["steak","chicken","fish"]
shoppinglist.extend(proteinlist)
print("using extend")
print(shoppinglist)
Notebook Output

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.

#Use extend to add a collection to a list
shoppinglist = ["steak","chicken","fish"]
shoppinglist.insert(1,"detergent")
print("using insert")
print(shoppinglist)
Notebook Output

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.

shoppinglist = ["milk","bread","apples","eggs","rice"]
#Use pop to remove an item by index
print("use pop")
shoppinglist.pop(0)
print(shoppinglist)
Notebook Output

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.

shoppinglist = ["milk","bread","apples","eggs","rice"]
print("use delete")
del shoppinglist[0]
print(shoppinglist)

Remove an item from a Python List by value

To remove an item from a Python List by value use the remove() method.

shoppinglist = ["milk","bread","apples","eggs","rice"]
print("use remove")
shoppinglist.remove("bread")
print(shoppinglist)
Notebook Output

Remove all items from a Python List (empty a list)

To remove all items from a Python List use the clear() method.

print("use clear")
shoppinglist = ["milk","bread","apples","eggs","rice"]
shoppinglist.clear()
print(shoppinglist)
Notebook Output