Chevron Left Icon
Python Lists and Dictionaries
Lesson 1 of 2
Chevron Right Icon
  1. 1
    Lists are a datatype you can use to store a collection of different pieces of information as a sequence under a single variable name. (Datatypes you’ve already learned about include strings, numb…
  2. 2
    You can access an individual item on the list by its index. An index is like an address that identifies the item’s place in the list. The index appears directly after the list name, in between br…
  3. 3
    A list index behaves like any other variable name! It can be used to access as well as assign values. You saw how to access a list index like this: zoo_animals[0] # Gets the value “pangolin” Yo…
  4. 4
    A list doesn’t have to have a fixed length. You can add items to the end of a list any time you like! letters = [‘a’, ‘b’, ‘c’] letters.append(‘d’) print len(letters) print letters 1. In the abo…
  5. 5
    Sometimes, you only want to access a portion of a list. Consider the following code: letters = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’] slice = letters[1:3] print slice print letters What is this code doing? F…
  6. 6
    You can slice a string exactly like a list! In fact, you can think of strings as lists of characters: each character is a sequential item in the list, starting from index 0. my_list[:2] # Grabs th…
  7. 7
    Sometimes you need to search for an item in a list. animals = [“ant”, “bat”, “cat”] print animals.index(“bat”) 1. First, we create a list called animals with three strings. 2. Then, we print the…
  8. 8
    If you want to do something with every item in the list, you can use a for loop. If you’ve learned about for loops in JavaScript, pay close attention! They’re different in Python. for variable in …
  9. 9
    If your list is a jumbled mess, you may need to sort() it. animals = [“cat”, “ant”, “bat”] animals.sort() for animal in animals: print animal 1. First, we create a list called animals with th…
  10. 10
    A dictionary is similar to a list, but you access values by looking up a key instead of an index. A key can be any string or number. Dictionaries are enclosed in curly braces, like so: d = {‘ke…
  11. 11
    Like Lists, Dictionaries are mutable. This means they can be changed after they are created. One advantage of this is that we can add new key/value pairs to the dictionary after it is created l…
  12. 12
    Because dictionaries are mutable, they can be changed in many ways. Items can be removed from a dictionary with the del command: del dict_name[key_name] will remove the key key_name and its asso…
  13. 13
    Sometimes you need to remove something from a list. beatles = [“john”,”paul”,”george”,”ringo”,”stuart”] beatles.remove(“stuart”) print beatles This code will print: [“john”,”paul”,”george”,”ri…
  14. 14
    Let’s go over a few last notes about dictionaries my_dict = { “fish”: [“c”, “a”, “r”, “p”], “cash”: -4483, “luck”: “good” } print my_dict[“fish”][0] 1. In the example above, we created a…
  1. 1
    Before we begin our exercise, we should go over the Python for loop one more time. For now, we are only going to go over the for loop in terms of how it relates to lists and dictionaries. We’ll…
  2. 2
    You can also use a for loop on a dictionary to loop through its keys with the following: # A simple dictionary d = {“foo” : “bar”} for key in d: print d[key] # prints “bar” Note that dic…
  3. 3
    The blocks of code in a for loop can be as big or as small as they need to be. While looping, you may want to perform different actions depending on the particular item in the list. numbers = [1,…
  4. 4
    Functions can also take lists as inputs and perform various operations on those lists. def count_small(numbers): total = 0 for n in numbers: if n < 10: total = total + 1 return t…
  5. 5
    As we’ve mentioned, strings are like lists with characters as elements. You can loop through strings the same way you loop through lists! While we won’t ask you to do that in this section, we’v…
  6. 6
    Okay—on to the core of our project. Congratulations! You are now the proud owner of your very own Codecademy brand supermarket. animal_counts = { “ant”: 3, “bear”: 6, “crow”: 2 } In the e…
  7. 7
    Good work! As a store manager, you’re also in charge of keeping track of your stock/inventory.
  8. 8
    Now that you have all of your product info, you should print out all of your inventory information. once = {‘a’: 1, ‘b’: 2} twice = {‘a’: 2, ‘b’: 4} for key in once: print “Once: %s” % once[key…
  9. 9
    For paperwork and accounting purposes, let’s record the total value of your inventory. It’s nice to know what we’re worth!
  10. 10
    Great work! Now we’re going to take a step back from the management side and take a look through the eyes of the shopper. In order for customers to order online, we are going to have to make a co…
  11. 11
    Good! Now you’re going to need to know how much you’re paying for all of the items on your grocery list. def sum(numbers): total = 0 for number in numbers: total += number return total …
  12. 12
    Now you need your compute_bill function to take the stock/inventory of a particular item into account when computing the cost. Ultimately, if an item isn’t in stock, then it shouldn’t be included …
  13. 13
    Perfect! You’ve done a great job with lists and dictionaries in this project. You’ve practiced: Using for loops with lists and dictionaries Writing functions with loops, lists, and diction…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo