Python for Introductory StatisticsXYZ Homework Edition

⇩ Download ▾

2.1 Creating and Accessing Lists

Example: Create a Python list of 24, 22, 5, 34, 81, and 10. Save the list to the variable name my_list and display it.

  1. Click + Code to add a code-cell
  2. Type my_list = [24,22,5,34,81,10]
  3. Type my_list
  4. Click play button
my_list = [24,22,5,34,81,10]
my_list
Show expected output
[24, 22, 5, 34, 81, 10]
Diagram of my_list = [24, 22, 5, 34, 81, 10]: a label points to my_list as the list name and to the bracketed values as the items.

Example: Create a list named x using the following integers: 10, 14, 20 and 15. Print the list.

x = [10,14,20,15]
x
Show expected output
[10, 14, 20, 15]

Example: Create a list named x using the following colors: red, yellow, green and blue. Print the list.

y = ['red','yellow','green','blue']  # string data in quotes
y
Show expected output
['red', 'yellow', 'green', 'blue']

List elements are indexed, and we can use the index operator to access its elements. The first element has index 0, the second element has index 1 etc.

Example: Create a list of 24, 22, 5, 34, 81 and 10. Save the list in x and display the entire list.

Diagram of the list x = [24, 22, 5, 34, 81, 10] drawn as six boxes, with arrows from each box down to its index position 0 through 5.
x = [24, 22, 5, 34, 81, 10]
x
Show expected output
[24, 22, 5, 34, 81, 10]

Example: From the list x above, display the first element. See code below.

x[0]
Show expected output
24

Example: From the list x above, display the third element. See code below.

x[2]
Show expected output
5

Example: From the list x above, display the last element?

  1. Click + Code
  2. Type x = [24,22,5,34,81,10]
  3. Type x[-1]
  4. Click play button
x = [24,22,5,34,81,10]

x[-1]
Show expected output
10

Note: The index for the last element of a list is 1-1, the second from the last is 2-2, the third from the last is 3-3, etc

Example: Create a list named z using the following items: Monday, Tuesday, Wednesday, 4, 5, 6, and 7.0. Here, we have string, integer and float data types.

z = ['Monday', 'Tuesday', 'Wednesday', 4, 5, 6, 7.0]

To display the whole list, enter its name or use the print function.

print(z)
Show expected output
['Monday', 'Tuesday', 'Wednesday', 4, 5, 6, 7.0]
z[0]      # the first item
Show expected output
'Monday'
z[-1]    # the last item
Show expected output
7.0
print(z[6])     # the 7 th item
print(z[-2])    # the second from the last
Show expected output
7.0
6

Note: To display multiple items of a Python list, use the slicing operator [a:b] from a to b-1. For example, z[0:3] will use index 0, 1, and 2. Notice 3 is not included.

Example: Display the first 3 elements of z.

print(z[0:3])
Show expected output
['Monday', 'Tuesday', 'Wednesday']

An Empty List

A list that contains no elements is called an empty list; you can create one with empty brackets,

x =

x=[]
x
Show expected output
[]

Lists are mutable

Elements of a list can be changed (replaced).

Example: From the list, x=[10,20,30], replace the second element, x[1]=20, with the letter a.

x = [10, 20, 30]
print(x)
x[1] = 'a'   # assign 'a' to the second element (replace 20 with 'a')
print(x)
Show expected output
[10, 20, 30]
[10, 'a', 30]

Adapted from Python for Introductory Statistics, by Simon Aman (Truman College, City Colleges of Chicago), licensed under CC BY 4.0. Changes were made: reformatted as an accessible XYZ web edition with live in-browser code cells. License: CC-BY-4.0.

These eBooks are a prerelease and are not yet certified conformant with WCAG 2.1 AA or ADA Title II. Every page is built against an automated accessibility gate, and the published editions will meet ADA Title II requirements when they release in late September 2026. If something is unusable, please tell us.