LING 398
Elsayed Issa
Jingying Hu
Spring 2026

Lecture Plan (75 minutes):¶

  • Questions from last lecture (10 minutes)

  • Sets (15 minutes)

  • Tuples (15 minutes)

  • Dictionaries (15 minutes)

  • Questions (5 minutes)

Sets

  • Sets describe an unordered collection of unique objects.

  • They filter out repeated items.

  • A Set is created using curly brackets {}

In [ ]:
my_l = [5, 9.4, "car", True, "car"]
my_l
In [ ]:
my_set = {5, 9.4, "car", True, "car"}
print(my_set)
type(my_set)
  • We can use the set() method as well
In [ ]:
# Imagine we have the follwoing list, we can convert it to a set
# NOTICE: repeated items are ignored
my_list = [1, 2, 3, 4, 1, 5, 5, 2, 3, 6, 7]
set(my_list)

Some set operations¶

  • Sets are known for some operations such as intersection, union, difference and symmetric difference
In [ ]:
# let's declare two sets
set1 = {1, 2, 3, 4}
set2 = {3, 4, 5, 6}
In [ ]:
# Union 
a1 = set1 | set2 
print("a1:", a1)

a2 = set1.union(set2)
print("a2:", a2) 
In [ ]:
# intersection
a1 = set1 & set2  
print("a1:", a1)

a2 = set1.intersection(set2)
print("a2:", a2)
In [ ]:
# difference
a1 = set1 - set2  
print(a1)

a2 = set1.difference(set2)
print(a2)
In [ ]:
set2.difference(set1)
In [ ]:
# symmetric difference
a1 = set1 ^ set2  
print(a1)

a2 = set1.symmetric_difference(set2)
print(a2)
  • Other methods include add(), remove(), et cetera.

  • See the sets notebook for more methods.

Tuples

  • A tuple is very similar to a list.

  • It can store a sequence of arbitrary objects (a mix of numbers, strings, lists, other tuples, etc.).

  • Tuples use parentheses ()

In [ ]:
# tuple with 3 entries
my_tuple = (1, "a", None, ['a', 'b'], {1,2,3})  

print(type(my_tuple))

isinstance(my_tuple, tuple)
  • Tuples are immutable meaning that once they are created, they cannot be changed.
In [ ]:
# Lists are mutabale
example_list = [1, "hi", None]
example_list[0] = 2
example_list
In [ ]:
# Tuples are immutable
example_tuple = (1, "hi", None)
example_tuple[0] = 2 # TypeError: 'tuple' object does not support item assignment
example_tuple
  • we use the built-in function tuple() to convert any other iterable to a tuple
In [ ]:
# This is a list
a = [1, "hi", None]
print(type(a))
In [ ]:
# Now it is a tuple
b = tuple(a)
print(type(b))

Indexing and Slicing¶

  • Indexing means accessing individual items from a sequence by specifying the index of that item.
  • seq[start, stop, step]
In [ ]:
# get an item by a postive index
a = (1, "hi", "45", "ma", 34)
a[2]
In [ ]:
# get an item by a negative index
a = (1, "hi", "45", "ma", 34)
a[-1]
In [ ]:
# get a subsequence by identifying start-index and stop-index
a = (1, "hi", "45", "ma", 34)
a[0:3]
In [ ]:
# get a subsequence after 2 index 
# (Notice the 2 index will be included)
a = (1, "hi", "45", "ma", 34)
a[2:]
In [ ]:
# get a subsequence before 2 index 
# (Notice the 2 index will NOT be included)
a = (1, "hi", "45", "ma", 34)
a[:2]
In [ ]:
# get a subsequence before -2 index 
# (Notice the 2 index will be included)
a = (1, "hi", "45", "ma", 34)
a[-2:]
In [ ]:
# get a subsequence before 2 index 
# (Notice the 2 index will NOT be included)
a = (1, "hi", "45", "ma", 34)
a[:-2]
In [ ]:
# Now if we can use seq[start, stop, step]
# a = (1, "hi", "45", "ma", 34)
# a[0:2:1]
In [ ]:
a = (1, "hi", "45", "ma", 34)
a[::-1]

Packing and Unpacking Tuples¶

  • This is a very importat feature about tuple assignment.

  • This feature assigns the right-hand side of values into the left-hand side.

  • In packing: values are included into a new tuple.

  • In unpacking: those values are extracted into a single variable.

In [ ]:
shamiso = "cars"
shamiso
In [ ]:
jeffery = "Gardians of the Galaxy: Falling 3"
jeffery
In [ ]:
qasim = "Kung Fu Panda"
qasim
In [ ]:
# movies is a variable that we assign a tuple of strings to.
### PACKED TUPLE
movies = ("cars", "Gardians of the Galaxy: Falling 3", "Kung Fu Panda")
len(movies)
In [ ]:
# UNPACK THE TUPLE
shamiso, jeffery, qasim, ronald = movies
In [ ]:
our_class = (["Shamiso", "cars"], ["Jeffery", "Gardians of the Galaxy: Falling 3"], ["Qasim", "Kung Fu Panda"])

type(our_class)
In [ ]:
shamiso, jeffery, qasim = our_class
print(shamiso)
print()
print(jeffery)
print()
print(qasim)
In [ ]:
type(shamiso)
In [ ]:
jeffery = ['Jeffery', 'Gardians of the Galaxy: Falling 3']
jeffery
In [ ]:
# packing values
example_tuple = ("John Thomas", 1980, "3 idiots", "xx") 
#len(example_tuple)
 
# unpacking values
name, year, movie, xx = example_tuple
 
# # print name
print(name)
 
# # print year
print(year)
 
# # print movie
print(movie)

xx
In [ ]:
# 
cling_class = (["Jingyang", "Ronald"], ["Kung Fu Panda", "Wall-E"])

people, movies = cling_class
print(people)
print(movies)

# print("Claire likes the green book")
In [ ]:
cling_class[0][0]
In [ ]:
f"{cling_class[0][0]} likes {cling_class[1][0]}"
In [ ]:
f"{cling_class[0][1]} likes {cling_class[1][1]}"

Dictionaries

  • Dictionaries store key-value pairs. Keys allow us to quickly retrieve values.

  • Each key maps to one value.

  • I will create a dictionary that includes information about a person.

Key Value
name Derek John
age 35
hobbies ['swimming', 'diving', 'running']
movies ('John Wick', 'The Shawshank Redemption', 'Ex Machina')

Create a dictionary¶

  • we create a dictionary by mapping the key to its value separated by a comma in curly brackets {}

  • my_dictionary = {"key":"value"}

  • {key1:value1, key2:value2, ...}

In [ ]:
# we create a dictionary by mapping the key to its value
my_dictionary = {"name": "Derek John", "age": 35}
print(my_dictionary)
type(my_dictionary)
In [ ]:
# Another way of creating a dictionary is using list of tuples 
# inside `dict()`
my_dict = dict([("name", "Derek John"),("age", 35)])
my_dict

Adding key-value mappings¶

  • Now we add hobbies and movies in the table above to our dictionary

  • We do this by the following syntax: my_dictionary[new_key] = new_value

  • Or using update() method.

In [ ]:
# First Option
my_dictionary['hobbies'] = ['swimming', 'diving', 'running']
my_dictionary
In [ ]:
# Second Option using update()
my_dictionary.update([("movies", ('John Wick', 'The Shawshank Redemption', 'Ex Machina'))])
my_dictionary
In [ ]:
# declair empty dict
my_dict = {} # dict()
my_dict
In [ ]:
my_dict['names'] = ["Qasim", "Ronald", "Jeffery", "Shamiso", "Jingyang"]
In [ ]:
my_dict
In [ ]:
my_dict['age'] = (23, 46, 47, 65, 122)
In [ ]:
my_dict

Retrive a value given the key¶

In [ ]:
# let's find all the movies that Derek watches
my_dict['names']
In [ ]:
# let's get his age
In [ ]:
my_dict['age']

Important method to get keys and values¶

In [ ]:
'name' in my_dictionary

len()¶

  • counts the number of keys
In [ ]:
len(my_dictionary)

keys()¶

  • get all dictionary keys
In [ ]:
my_dictionary.keys()
In [ ]:
list(my_dictionary.keys())

values()¶

  • get all dictionary values
In [ ]:
my_dictionary.values()
In [ ]:
list(my_dictionary.values())

items()¶

  • get all dictionary key-value items
In [ ]:
# get dict items
my_dictionary.items()
In [ ]:
# convert them to a list of tuples
list(my_dictionary.items())
In [ ]:
# access the first tuple or the tuple at index 0
dict_items = list(my_dictionary.items())
dict_items[0]
In [ ]:
# access the second tuple or the tuple at index 1
dict_items[1]
In [ ]:
# # access the second item in the tuple 
dict_items[1][1]