PYTHON LESSON 5: Python Lists & Dictionaries (Part 2)

For One and All;

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 list_name: \n # Do stuff!

A variable name follows the for keyword; it will be assigned the value of each list item in turn.

 \n Then in list_name designates list_name as the list the loop will work on. The line ends with a colon (:) and the indented code that follows it will be executed once per item in the list.

 \n If your list is a jumbled mess, you may need to sort() it.

animals = ["cat", "ant", "bat"] \n animals.sort() \n \n for animal in animals: \n print animal
  1. First, we create a list called animals with three strings. The strings are not in alphabetical order.
  2. Then, we sort animals into alphabetical order. Note that .sort() modifies the list rather than returning a new list.
  3. Then, for each item in animals, we print that item out as "ant", "bat", "cat" on their own line each.

This Next Part is Key;

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 = {'key1' : 1, 'key2' : 2, 'key3' : 3}

This is a dictionary called d with three key-value pairs. The key 'key1' points to the value 1, 'key2' to 2, and so on.

Accessing dictionary values by key is just like accessing list values by index:

residents['Puffin'] \n # Gets the value 104

Dictionaries are great for things like phone books (pairing a name with a phone number), login pages (pairing an e-mail address with a username), and more!

New Entries;

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 like so:

dict_name[new_key] = new_value

An empty pair of curly braces {} is an empty dictionary, just like an empty pair of [] is an empty list.

The length len() of a dictionary is the number of key-value pairs it has. Each pair counts only once, even if the value is a list. (That’s right: you can put lists inside dictionaries!)

Changing Your Mind;

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 associated value from the dictionary.

A new value can be associated with a key by assigning a value to the key, like so:

dict_name[key] = new_value

Remove a Few Things;

Sometimes you need to remove something from a list.

beatles = ["john","paul","george","ringo","stuart"] \n beatles.remove("stuart") \n print beatles

This code will print:

["john","paul","george","ringo"]
  1. We create a list called beatles with 5 strings.
  2. Then, we remove the first item from beatles that matches the string "stuart". Note that .remove(item) does not return anything.
  3. Finally, we print out that list just to see that "stuart" was actually removed.

Endnotes;

Let’s go over a few last notes about dictionaries

my_dict = { \n "fish": ["c", "a", "r", "p"], \n "cash": -4483, \n "luck": "good" \n } \n print my_dict["fish"][0]
  1. In the example above, we created a dictionary that holds many types of values.
  2. The key "fish" has a list, the key "cash" has an int, and the key "luck" has a string.
  3. Finally, we print the letter "c". When we access a value in the dictionary like my_dict["fish"], we have direct access to that value (which happens to be a list). We can access the item at index 0 in the list stored by the key "fish".