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.
- Click
+ Codeto add a code-cell - Type
my_list = [24,22,5,34,81,10] - Type
my_list - Click
playbutton
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.](photos/ch02-img01.png)
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.](photos/ch02-img02.png)
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
24Example: From the list x above, display the third element. See code below.
x[2]
Show expected output
5Example: From the list x above, display the last element?
- Click
+ Code - Type
x = [24,22,5,34,81,10] - Type
x[-1] - Click
playbutton
x = [24,22,5,34,81,10]
x[-1]
Show expected output
10Note: The index for the last element of a list is , the second from the last is , the third from the last is , 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.0print(z[6]) # the 7 th item
print(z[-2]) # the second from the last
Show expected output
7.0
6Note: 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.