Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Comparing lists, tuples and maps
# a crude program to show examples of a tuple
# and relate to lists and strings
# a tuple is similar to a list, but uses parentheses
fibs = (0, 1, 1, 2, 3)
print(fibs[3])
# Above, we define the variable fibs as the numbers 0, 1, 1, 2, and 3.
# Just like a list, we print the item in the index position 3 in the tuple.
# The main difference between a tuple and a list is that you cannot
# change the tuple once you've created it.
# For example, if we try to replace an index position value in the tuple,
# we get an error message.
# fibs[0] = 4
# If we cannot change a tuple once we've created it,
# Why would we use a tuple instead of a list?
# I'm glad you asked,
# primarily, it's because sometimes it is useful to use something
# that you know can never change.
# If you create a tuple with two elements inside, it will
# always have those two elements.
# We can see the importance if we call on particular specifications (a tuple created in another program) of something in a program
# we are currently working in. In this way, tuples are more efficient than using a list. -we'll dive into that later.
# Python Maps Won't Help You Find Your Way -adapted from Jason Briggs Python for Kids
# In Python, a MAP (also referred to as a dict (or dictionary)
# is a collection of things, like lists and tuples.
# The difference between maps, tuples, and lists:
# each item in a map has a KEY and a corresponding VALUE.
# For example, say we have a list of company brands and what those brands make:
things_brands_make = ['IKEA, furniture', 'Ralph Lauren, clothing', 'General Motors, cars', 'Deer Park, bottled water', 'BIC, writing tools']
# Above, we put the information into a Python list, with the brand followed
# by what stuff that brand makes.
# If you were asked what things the brand, BIC makes, you can scan the list and find
# the answer to be writing tools.
# But, what if the list included 200 (or more) brands?
# -That would take up a lot more time!
# So, if we store the information in a map, with the brand's name as the key
# and what they make as the value,
# The Python code would look like this:
things_brands_make = {'IKEA' : 'furniture', 'Ralph Lauren' : 'clothing', 'General Motors' : 'cars', 'Deer Park' : 'bottled water', 'BIC' : 'writing tools'}
# We use colons to separate each key from its value, and each key and value is sectioned of wih single quotes.
# Notice too, the items in the map are enclosed by braces ({}),
# not parentheses, like a tuple, and not brackets like a list.
# The result of a map (each key maps to a particular value), can visualized as a table:
# | Brand Name | Value |
# ______________ _____________
# | IKEA | furniture |
# | Ralph Lauren | clothing |
# | General Motors | cars |
# | Deer Park | bottled water |
# | Bic | writing tools |
# Now, to get the type of things the brand, BIC makes,
# we access our map "things_brands_make" using the brand's name as the key,
# like this:
print(things_brands_make['BIC'])
# Python gives the answer, writing tools.
# To delete a value in the map, use its key.
# For example, I'll remove Deer Park:
del things_brands_make['Deer Park']
print(things_brands_make)
# Now, the key, Deer Park and its value, bottled water is not part of the map
# To replace a value in a map, we also use its key:
things_brands_make['Ralph Lauren'] = 'shoes'
print(things_brands_make)
# In other words, we replaced the thing that a brand makes with shoes
# by using the key Ralph Lauren.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.