0% found this document useful (0 votes)
1 views11 pages

22 Lists

The document provides an overview of lists in programming, explaining key concepts such as elements, indexes, and list initialization. It covers accessing and modifying list elements, handling out-of-bounds errors, and using loops to iterate through lists. Additionally, it lists various list functions and presents a 'list mystery' problem to illustrate list traversal and manipulation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views11 pages

22 Lists

The document provides an overview of lists in programming, explaining key concepts such as elements, indexes, and list initialization. It covers accessing and modifying list elements, handling out-of-bounds errors, and using loops to iterate through lists. Additionally, it lists various list functions and presents a 'list mystery' problem to illustrate list traversal and manipulation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

CEN217, Fall 2019

Lecture 22: lists


Adapted from slides by Marty Stepp and Stuart Reges
Lists
• list: object that stores many values.
• element: One value in a list.
• index: A 0-based integer to access an element from an list.
index 0 1 2 3 4 5 6 7 8 9
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1
value 12 49 -2 26 5 17 -6 84 72 3

element 0 element 4 element 9


List initialization
name = [value, value, … value]
• Example:
numbers = [12, 49, -2, 26, 5, 17, -6]
index 0 1 2 3 4 5 6

value 12 49 -2 26 5 17 -6

• Useful when you know what the list's elements will be

name = [value] * count


• Example:
numbers = [0] * 4 index 0 1 2 3

value 0 0 0 0
List initialization

name = [value]* size


• Example:
numbers = [0]*3
Creates the following list
index

value 0 0 0
Accessing elements
name[index] # access
name[index] = value # modify

• Example:
numbers = [0] * 2
numbers[0] = 27
numbers[1] = -6
print(numbers[0])
if (numbers[1] < 0):
print("Element 1 is negative.")
index 0 1

value 27 -6
Out-of-bounds
• Legal indexes to use []: between – list's length and the list's length - 1.
• Reading or writing any index outside this range with [] will cause an
IndexError: list assignment index out of range
• Example:
data = [0] * 10
print(data[0]) # okay
print(data[9]) # okay
print(data[-20]) # error
print(data[10]) # error
index 0 1 2 3 4 5 6 7 8 9

value 0 0 0 0 0 0 0 0 0 0
Lists and for loops
• It is common to use for loops to access list elements.
for i in range(0, 8):
print(str(numbers[i]) + " ", end='')
print() # output: 0 4 11 0 44 0 0 2
• Sometimes we assign each element a value in a loop.
for i in range(0, 8):
numbers[i] = 2 * i
index 0 1 2 3 4 5 6 7

value 0 2 4 6 8 10 12 14
len()
• Use len() to find the number of elements in a list.

for i in range(0, len(numbers)):


print(numbers[i] + " ", end='')
# output: 0 2 4 6 8 10 12 14

• What expressions refer to:


• The last element of any list?
• The middle element?
Lists and for loops
• You can also loop directly over lists, just as with strings
list = [1, 3, 6, 23, 43, 12]
for number in list:
print(str(number + " ", end='')
print() # output: 1 3 6 23 43 12
List functions
Function Description
append(x) Add an item to the end of the list. Equivalent to a[len(a):] = [x].
extend(L) Extend the list by appending all the items in the given list. Equivalent
to a[len(a):] = L
insert(i, x) Inserts an item at a given position. i is the index of the element before which to insert,
so a.insert(0, x) inserts at the front of the list.
remove(x) Removes the first item from the list whose value is x. Errs if there is no such item.
pop(i) Removes the item at the given position in the list, and returns it. a.pop() removes and
returns the last item in the list.
clear() Remove all items from the list.
index(x) Returns the index in the list of the first item whose value is x. Errs if there is no such item.
count(x) Returns the number of times x appears in the list.
sort() Sort the items of the list
reverse() Reverses the elements of the list
copy() Return a copy of the list.
"list mystery" problem
• traversal: An examination of each element of an list.

• What element values are stored in the following list?

a = [1, 7, 5, 6, 4, 14, 11]


for i in range(0, len(a) – 1):
if (a[i] > a[i + 1]):
a[i + 1] = a[i + 1] * 2

index 0 1 2 3 4 5 6

value 1 7 10 12 8 14 22

You might also like