Lists & Dictionaries

In this course, you will learn about the data structures lists and dictionaries.

StartPython Illustration
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: [...] You can see how assignment works on li...

  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! [...] 1. In the above example, we first create a list called [...] . 2. Then, we add the s...

  5. 5

    Sometimes, you only want to access a portion of a list. Consider the following code: [...] What is this code doing? First, we create a list called [...] . Then, we take a subsection of the li...

  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. [...] If your list ...

  7. 7

    Sometimes you need to search for an item in a list. [...] 1. First, we create a list called [...] with three strings. 2. Then, we print the first index that contains the string [...] , which...

  8. 8

    If you want to do something with every item in the list, you can use a [...] loop. If you've learned about [...] loops in JavaScript, pay close attention! They're different in Python. [...] ...

  9. 9

    If your list is a jumbled mess, you may need to [...] it. [...] 1. First, we create a list called [...] with three strings. The strings are not in alphabetical order. 2. Then, we sort [......

  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: [...] ...

  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 [...] command: [...] will remove the key [...] and its associated value f...

  13. 13

    Sometimes you need to remove something from a list. [...] This code will print: [...] 1. We create a list called [...] with 5 strings. 2. Then, we remove the first item from [...] that m...

  14. 14

    Let's go over a few last notes about dictionaries [...] 1. In the example above, we created a dictionary that holds many types of values. 2. The key [...] has a list, the key [...] has ...

  1. 1

    Before we begin our exercise, we should go over the Python [...] loop one more time. For now, we are only going to go over the [...] loop in terms of how it relates to lists and *dictionaries...

  2. 2

    You can also use a [...] loop on a dictionary to loop through its keys with the following: [...] Note that dictionaries are unordered, meaning that any time you loop through a dictionary,...

  3. 3

    The blocks of code in a [...] 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. [...] ...

  4. 4

    Functions can also take lists as inputs and perform various operations on those lists. [...] 1. In the above example, we define a function [...] that has one parameter, [...] . 2. We initi...

  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. [...] In the example above, we create a new dictionary called [...

  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. [...] 1. In the above example, we create two dictionaries, [...] and [...] , that have the...

  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. [...] 1. In the above example, we first define a function called [...] with a paramete...

  12. 12

    Now you need your [...] 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 in th...

  13. 13

    Perfect! You've done a great job with lists and dictionaries in this project. You've practiced: Using [...] loops with lists and dictionaries Writing functions with loops, lists, and dic...

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

Lists & Dictionaries

StartPython Illustration