Tuples & Lists

HSS 611: Programming for HSS

Taegyoon Kim

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

  • Tuple
  • String

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()

  • Adds an object to the end of the list
  • No need to re-assign
  • Once append() is run, the list is modified in memory

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()

  • Adds the elements of another list (or any iterable), to the end of the current list

List

extend()

  • What will it return?

List

extend()

  • How about this? Why does it work or not?

List

append() vs. extend()

List

+

  • To add lists together without modifying original lists, just +

List

Removing items

  • del(): use a specific index
  • Can also be used to remove the entire object
  • Return and then remove the last element

List

Removing items

  • remove(): remove a specific element
  • It removes the first item that matches the specified value
  • To remove all 7s, consider list comprehension (will be discussed soon)

List

Slicing

  • Lists can be sliced with the following syntax (similar to range()):

[start:stop:step]

  • start at start (default 0)

  • stop one step before stop (default is length of list)

  • step specifies how many indices to jump

List

Slicing

  • Get everything

List

Slicing

  • What will this return?

List

Slicing

  • What will this return?

List

Slicing

  • What will this return?

List

Slicing

  • Look how easy it is to get first n elements, and then the rest

List

Slicing

  • A few more examples

List

Slicing

  • A few more examples

Tuple

Tuples are also an ordered sequence of items

  • They are created, typically, with parentheses ()

  • Unlike lists, tuples are immutable

We can create one without parentheses too

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()

Tuple

Tuples are iterable

  • It is possible to have tuples of length 1

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()

String method

split()

  • Splits strings into a list of strings
  • If no character is specified, it will split by a white space

String method

strip, lstrip, rstrip

  • Trim from right (rstrip), left (lstrip), or both sides (strip)
  • Removes extra white space characters such as ' ', \t, \n

String method

strip, lstrip, rstrip

String method

strip, lstrip, rstrip

String method

strip, lstrip, rstrip

String method

String methods never modify in place

More on string

Indexing and slicing strings

  • Strings can be indexed and sliced just like lists
  • [start:stop:step] still works

More on string

Indexing and slicing strings

More on string

Strings in for loops

  • Strings are immutable, but they are iterable

More on string

String length

  • The length of a string is its number of characters (including white spaces)

More on string

Strings and lists

  • Convert string to list (list())
  • Returns a list with every character in string
  • We also saw this gives a list

More on string

Strings and lists

  • One can also join a list of elements into a string (e.g., when you want to lump multiple sentences)
  • Could join by another string too

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

  • With for loop

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”

List comprehension

  • More advanced example
  • Combine two digits to form a two-digit number, if the number is divisible by 3
  • Need to iterate over two lists