Lists Data Types

Lists store multiple values. The values could be the same data type or different data types.

Learning Objectives

You should be able to:

  • Use lists to store multiple values
  • Determine the number of items in a list
  • Insert, update, and remove list items

Video Walkthrough

Use this video to follow along with the steps in this lab.

Lists

A string object just holds one piece of text. An integer holds a single whole number. A float holds a single decimal number. Lists can hold multiple values. This section will walk you through several examples of creating and using lists.

  • In a terminal, run python to start an interactive Python shell.
python
  • Run the following code to create a list variable named names with 4 values: Alice, Bob, Eve, and the number 42. Yes, 42 would be a strange name.
names = ["Alice", "Bob", "Eve", 42]
  • Be sure to put the commas outside of the quotation marks. It is important to note that Python lists can contain different data types. This is different than the default behavior in many programming languages that require all elements to be the same data type.

  • Check the number of items in the list with the len() function. Yes, this is the same function that is used to check the number of characters in a string.

len(names)

The result should be 4.

  • You can use the print() function to show the values in the list. This is similar to printing the value of a string.
print(names)

The result will be: ['Alice', 'Bob', 'Eve', 42].

  • Bob has been annoying lately, so remove him from the list with the remove() method.
names.remove("Bob")

But did that really remove Bob? Running upper() and lower() on a string variable does not change the variable's value.

  • Print the value of names again.
print(names)

There should only be 3 values left. Bob, indeed, has been removed.

  • Assume that Bob has apologized for being annoying, so we want to let him back in. The append() method can be used to add values to the end of a list.
names.append("Bob")
print(names)

Bob's back. But he's at the end of the list, now.

  • If you have somehow messed up your list, you can re-create it with the following code.
names = ["Alice", "Eve", 42, "Bob"]
  • Individual values can be accessed by their index. Like strings, indexes start at 0.
print(names[0])
Alice
print(names[1])
Eve
print(names[2])
42
print(names[3])
Bob
print(names[4])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range
  • Lastly, it is possible to have lists of lists.
schools = [ ["South High", "Somecity"], ["North High", "OtherCity"]]
print(schools)

This code contains a schools variable that is a single list. The schools list has 2 items, and each of the items is a list with the school name and the city name.

Lists are very versatile and powerful. To master Python, you must master the list data type.

Challenge

  • Make a list of companies where you might want to work in the future.
  • Display the number of companies in the list.
  • Remove your least favorite company.
  • Add another company that your parents would like you to work for.
  • Display the list again.

Reflection

  • How are list data types different from strings?
  • How could lists help you organize things you have to get done? How would you use lists of lists?

Key Terms

  • Data Type: List: A data structure in Python that is used to store an ordered collection of items. Lists are mutable, meaning their elements can be changed after the list is created. They can contain items of different data types, including other lists. Lists are defined using square brackets [], with elements separated by commas.