INTRO O
PROBLEM SOLVING AND
PROGRAMMING IN PYTHON
(use the Space key to navigate through all slides)

Prof. Andrea Gallegati |
Prof. Dario Abbondanza |

CIS 1051 - LISTS

Understanding the
objects
Collecting ORDERED Items
list
A list is an ordered collection of items (objects)
- Created using square brackets,
numbers = [1, 2, 3, 7, 300]
-
they are mutable, you can change, add or remove elements without creating a new list
numbers[3] = 0
numbers = numbers + ['hi']
numbers.remove(300)
Indexing and slicing
numbers = [1, 2, 4, 17, -8, 300]
# Access elements by index
numbers[0]
numbers[3]
# Negative indexing
numbers[-1]
# Slicing the list
# to get sub-lists
start = 2
end = 4
numbers[start:end]
To access a subset of its elements
Common list methods
-
append(x): Add x to the end of the list.
-
insert(index, x): Insert x at a specific index.
-
remove(x): Remove the first occurrence of x.
-
pop(index): Remove and return the item at index (default is the last item).
-
sort(): Sort the list in place.
-
count(x): Count how many times x appears in the list.
- index(x): Return the index of the first occurrence of x.
Functions that are proper of the list class
Common list methods
Functions that are proper of the list class
Iteration
Is commonly used with lists
numbers = [1, 2, 4, 17, -8, 300]
for n in numbers:
print(n)
This was crafted with
A Framework created by Hakim El Hattab and contributors
to make stunning HTML presentations
lists
By Andrea Gallegati
lists
- 25