Python Lists and Dictionaries
Lesson 1 of 2
  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…

What you'll create

Portfolio projects that showcase your new skills

How you'll master it

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