Key Concepts

Review core concepts you need to learn to master this subject

Lists

primes = [2, 3, 5, 7, 11] print(primes) empty_list = []

In Python, lists are ordered collections of items that allow for easy use of a set of data.

List values are placed in between square brackets [ ], separated by commas. It is good practice to put a space between the comma and the next value. The values in a list do not need to be unique (the same value can be repeated).

Empty lists do not contain any values within the square brackets.

Introduction to Lists
Lesson 1 of 2
  1. 1
    In programming, it is common to want to work with collections of data. In Python, a list is one of the many built-in data structures that allows us…
  2. 2
    Lists can contain more than just numbers. Let’s revisit our classroom example with heights: - Noelle is 61 inches tall - Ava is 70 inches tall - Sam is 67 inches tall - Mia is 64 inches tall Inst…
  3. 3
    A list doesn’t have to contain anything. You can create an empty list like this: empty_list = [] Why would we create an empty list? Usually, it’s because we’re planning on filling it up later ba…
  4. 4
    As we start exploring lists further in the next exercises, we will encounter the concept of a method. In Python, for any specific data-type ( strings, booleans, lists, etc. ) there is built-in …
  5. 5
    We can add a single element to a list using the .append() Python method. Suppose we have an empty list called garden: garden = [] We can add the element “Tomatoes” by using the .append() method…
  6. 6
    When we want to add multiple items to a list, we can use + to combine two lists (this is also known as concatenation). Below, we have a list of items sold at a bakery called items_sold: items_sold…
  7. 7
    We are interviewing candidates for a job. We will call each candidate in order, represented by a Python list: calls = [“Juan”, “Zofia”, “Amare”, “Ezio”, “Ananya”] First, we’ll call “Juan”, then “…
  8. 8
    What if we want to select the last element of a list? We can use the index -1 to select the last item of a list, even when we don’t know how many elements are in a list. Consider the following li…
  9. 9
    Let’s return to our garden. garden = [“Tomatoes”, “Green Beans”, “Cauliflower”, “Grapes”] Unfortunately, we forgot to water our cauliflower and we don’t think it is going to recover. Thankful…
  10. 10
    We can remove elements in a list using the .remove() Python method. Suppose we have a filled list called shopping_line that represents a line at a grocery store: shopping_line = [“Cole”, “Kip”, “…
  11. 11
    We’ve seen that the items in a list can be numbers or strings. Lists can contain other lists! We will commonly refer to these as two-dimensional (2D) lists. Once more, let’s look at a class heig…
  12. 12
    Let’s return to our classroom heights example: heights = [[“Noelle”, 61], [“Ali”, 70], [“Sam”, 67]] Two-dimensional lists can be accessed similar to their one-dimensional counterpart. Instead of…
  13. 13
    Now that we know how to access two-dimensional lists, modifying the elements should come naturally. Let’s return to a classroom example, but now instead of heights or test scores, our list stores…
  14. 14
    So far, we have learned: - How to create a list - How to access, add, remove, and modify list elements - How to create a two-dimensional list - How to access and modify two-dimensional list elemen…

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