Skip to content

Sep 22: Lists, Sets, Dictionaries

Learning Objectives

After today's class, you should be able to:

  • Create a list and access a specific element using an index.
  • Describe operations that can be applied to a set of strings.
  • Create a dictionary of strings and look up values by key.

Announcements

  • Read Ch 5 – ideally:
    • 1st half before Friday
    • 2nd half before Monday
  • Submit HW 5 – ideally:
    • 1 and 2 before Friday
    • 3 and 4 before Monday

POGIL Activity

  • Containers
    • If you are absent today, complete this activity at home
    • Bring your completed activity to class or office hours

Example Code

Model 1

primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

Run each line one at a time; don't paste the entire block of code!

odd = [1, 3, 5, 7]
odd
odd[2]
odd[4]
len(odd)
number = odd[1]
number
odd[1] = 2
odd
number

Model 2

va_schools = {"JMU", "GMU", "VCU", "VT", "ODU", "WM", "UVA"}
two_letter = {"MU", "VT", "ND", "WM", "BC"}

Run each line one at a time; don't paste the entire block of code!

type(va_schools)
len(two_letter)
"ND" in va_schools
"JMU" not in two_letter
len(va_schools.union(two_letter))
va_schools.intersection(two_letter)
two_letter.difference(va_schools)
va_schools.remove("GMU")
va_schools.remove("ODU")
va_schools.remove("VCU")
print(va_schools)
va_schools.add("JMU")
va_schools.add("JMU")
va_schools.add("JMU")
print(va_schools)

Model 3

elements = {'C': 'carbon', 'H': 'hydrogen', 'O': 'oxygen', 'N': 'nitrogen'}

Run each line one at a time; don't paste the entire block of code!

type(elements)
elements.keys()
elements.values()
elements['C']
atom = 'N'
elements[atom]
elements[N]
elements['nitrogen']
elements[1]
len(elements)
elements['B'] = 'Boron'
elements.items()