Tuples & Lists
HSS 611: Programming for HSS
Sep 9, 2025
Agenda
- List
- Tuple
- String method
- More on string
- (List) comprehension
List
Lists are one of four major built-in data types to store collections of data
- The others are tuples, dictionaries, and sets
- Used to store “things” in a single variable
List
Items in a list do not need to be of the same type
List
Can initialize an empty list with just two squared brackets
List
Lists are ordered
- So you can access an item at a specific index
- This is called indexing/slicing
- Indexing is used to access a single element at a specific position
- Slicing is used to access a range of elements
List
Lists are ordered
- Sets are not ordered/indexed (this can be good!)
List
Lists are ordered
- The index can be negative too
List
Lists are ordered
- An item can occur more than once (unlike sets)
List
Lists are mutable
- Lists are mutable objects (so are sets and dictionaries)
- That means they can be modified once they are created
- For example
List
Many objects are immutable though
List
Length of a list
len()
returns the number of items in (or length of) a list
List
List methods
- In Python, methods are functions that belong to an object
- A function is a reusable block of code that performs a specific task
- So, methods are a subset of functions
- They only work with that object
- They are used with dot notation:
object.method()
List
List methods
- There are many useful methods that come with list objects
- We will be able to look at some of them
List
append()
- Often used inside for loops
List
insert()
- Adds an element at the specified position
- No reassignment
List
reverse()
- Reverses the order of the list
- Again, no reassignment
List
sort()
- Sorts the list (no re-assignment too)
List
sort()
- Elements are not always comparable to each other, but see the following
List
sorted()
function
sorted()
is a function but not a method
Returns a sorted version of the list, but original stays intact
List
index()
- Returns the index of the first element matching the specified value
List
extend()
- How about this? Why does it work or not?
List
+
- To add lists together without modifying original lists, just
+
List
Slicing
- Lists can be sliced with the following syntax (similar to
range()
):
start at start (default 0)
stop one step before stop (default is length of list)
step specifies how many indices to jump
List
Slicing
List
Slicing
List
Slicing
List
Slicing
List
Slicing
- Look how easy it is to get first n elements, and then the rest
List
Slicing
List
Slicing
Tuple
Tuples can be ordered and be indexed
Tuple
Used to conveniently swap variable values
Tuple
Used to return more than one value from a function
- It conveniently packages many values of different type into one object
Tuple
Tuples have just two methods: count() and index()
String method
Python has really rich string methods
- See a full list here
- Let’s explore some of them
String method
startswith()
, endswith()
, in
- Return a Boolean value
- Use
in
operator to see if a string ‘just contains’ a substring
String method
capitalize()
, title()
, upper()
, lower()
capitalize()
capitalizes the first character
title()
capitalizes the first character of every word
upper()
capitalizes everything
lower()
converts string to all lowercase
String method
capitalize()
, title()
, upper()
, lower()
List comprehension
Comprehensions
list
, set
, and dictionary
comprehensions
It is possible to create a list
, set
, or dictionary
using:
- for / while loops
- if / else statements in the loop
Comprehensions provide simple syntax to achieve it in a single line
Better readability too
List comprehension
Let’s look at speed: for loop vs. comprehension
List comprehension
Simple example: create a copy of a list
- Let’s do this with a for loop:
List comprehension
Create a copy of the list with list comprehension
List comprehension
The more common scenario is “do something” to every element of a list
List comprehension
The more common scenario is “do something” to every element of a list
- With list comprehension
- “Square the number, for each number in numbers”
List comprehension
Do something to element only if a condition is true
- New list with only even numbers squared
- With for loop
List comprehension
With list comprehension
- “Square the number, for each number in numbers, if the number is even”