Lists and Dictionaries


Lists

A list is a variable (with a name) that holds many elements of the same type addressed by indices.

Here’s how you make a list:

The above syntax includes declaration and initialization. It also includes information about the type of the array. Here’s some examples:

Let’s take a closer look at how lists work.

What is an index?

An index is the integer used to reference each element in a list.

The first element of a list is located at index 0, the second is at index 1, and so on.

NOTE: the length of an array does not equal the last index! Lists are “zero-indexed” meaning they start counting indices at 0. So the length of our list will always be the last index + 1.

To show this, a two element list would have the first element at index 0 and the second at index 1. However, since there are 2 elements, the length of the list is 2.

How do you access an element?

When you access an element, you can assign it to a variable to more easily access it later.

You can also change individual elements in a list without having to create an entirely new list!

Example:

A big thing to remember is that since lists are 0-indexed, meaning the index numbers start with the first one being 0, the last index of a list is the length of the list - 1, or len() - 1.

Adding and removing elements from a list

Adding an element onto the end of a list is called appending the element to the list. We do this by using the append() method and putting the elemnt you want to add inside append. For example:

To remove the last element from a list, you can use the pop() method with no arguement. If you want to remove an element at a specific index, simply reference that index in the pop method. Here’s an example of both:

Dictionaries

A dictionary is a data structure available in Python which lets you organize data into “key-value” pairs.

The basic syntax of a dictionary is shown below:

But what are keys and values exactly? Lets dive in a little deeper.

Keys

Put simply, a key is what you look up a value by. Keys can be any immutable type, such as str, int, float, bool, but not lists or other dicts for example. Each key is uniquely paired with their corresponding value, meaning you cannot have two identical keys in your dictionary.

Values

The value in a dictionary refers to the data being stored. Unlike keys, values can be of any type. It is possible for two values in a dictionary to be identical, provided their associated keys are different.

Inserting items into a dictionary

To construct a dictionary, you can declare it and initalize it in one go as shown below:

But it’s more useful to be able to add items after initializing your dictionary. To do so, reference the name of the dictionary followed by brackets surrounding whatever you want your key to be, then assign it to the corresponding value. For example,

Remove items from a dictionary

Similar to lists, you can remove an item from a dictionary by using the pop() method. To indicate which item you want to remove, use the key as the arguement for pop inside the parenthesis. In addition, the pop method will return the corresponding value.

Accessing and modifying items

To access an item in a dictionary, write the name of your dictionary followed by brackets which enclose the key for the item you want to accesss. You can change the value of an item by reassigning the key to a new value:

Sometimes it can be useful to modify a value based on what is was previously, an example of this is shown below.