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.

Adding Lists Together

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

In Python, lists can be added to each other using the plus symbol +. As shown in the code block, this will result in a new list containing the same items in the same order with the first list’s items coming first.

Note: This will not work for adding one item at a time (use .append() method). In order to add one item, create a new list with a single value and then use the plus symbol to add the list.

Python Lists: Data Types

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

In Python, lists are a versatile data type that can contain multiple different data types within the same square brackets. The possible data types within a list include numbers, strings, other objects, and even other lists.

List Method .append()

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

In Python, you can add values to the end of a list using the .append() method. This will place the object passed in as a new element at the very end of the list. Printing the list afterwards will visually show the appended value. This .append() method is not to be confused with returning an entirely new list with the passed object.

Aggregating Iterables Using zip()

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

In Python, data types that can be iterated (called iterables) can be used with the zip() function to aggregate data. The zip() function takes iterables, aggregates corresponding elements based on the iterables passed in, and returns an iterator. Each element of the returned iterator is a tuple of values.

As shown in the example, zip() is aggregating the data between the owners’ names and the dogs’ names to match the owner to their dogs. zip() returns an iterator containing the data based on what the user passes to the function. Empty iterables passed in will result in an empty iterator. To view the contents of the iterator returned from zip(), we can cast it as a list by using the list() function and printing the results.

List Item Ranges Including First or Last Item

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

In Python, when selecting a range of list items, if the first item to be selected is at index 0, no index needs to be specified before the :. Similarly, if the last item selected is the last item in the list, no index needs to be specified after the :.

List Method .count()

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

The .count() Python list method searches a list for whatever search term it receives as an argument, then returns the number of matching entries found.

Determining List Length with len()

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

The Python len() function can be used to determine the number of items found in the list it accepts as an argument.

Zero-Indexing

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

In Python, list index begins at zero and ends at the length of the list minus one. For example, in this list, 'Andy' is found at index 2.

List Method .sort()

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

The .sort() Python list method will sort the contents of whatever list it is called on. Numerical lists will be sorted in ascending order, and lists of Strings will be sorted into alphabetical order. It modifies the original list, and has no return value.

List Indices

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

Python list elements are ordered by index, a number referring to their placement in the list. List indices start at 0 and increment by one.

To access a list element by index, square bracket notation is used: list[index].

Negative List Indices

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

Negative indices for lists in Python can be used to reference elements in relation to the end of a list. This can be used to access single list elements or as part of defining a list range. For instance:

  • To select the last element, my_list[-1].
  • To select the last three elements, my_list[-3:].
  • To select everything except the last two elements, my_list[:-2].

List Slicing

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

A slice, or sub-list of Python list elements can be selected from a list using a colon-separated starting and ending point.

The syntax pattern is myList[START_NUMBER:END_NUMBER]. The slice will include the START_NUMBER index, and everything until but excluding the END_NUMBER item.

When slicing a list, a new list is returned, so if the slice is saved and then altered, the original list remains the same.

sorted() Function

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

The Python sorted() function accepts a list as an argument, and will return a new, sorted list containing the same elements as the original. Numerical lists will be sorted in ascending order, and lists of Strings will be sorted into alphabetical order. It does not modify the original, unsorted list.

  1. 1
    When we start writing more complex programs, we’ll start working with more pieces of data. But data can get messy real fast if we’re not careful. To keep our data tidy, we’ll want to use _data st…
  2. 2
    Lists order items so that they’re in a specific sequence. For example the comic strip, as a list, stores frames in a specific order. Without an order, the story wouldn’t make sense! This idea of…
  3. 3
    After a list is created, we’re able to add things to it. When we add things to the end of an existing list, we say that we’re appending them to the end. Imagine we’re trying out different endi…
  4. 4
    We’re also able to remove items from a list. Similar to adding items, we can modify lists by taking off the last item, or we can use indexing to select a specific item and remove it from the list…
  5. 5
    Great! Now that you know all about lists, let’s write some code. There are many ways to create lists and they can change depending on what language you are programming in. Here’s an example of a …
  6. 6
    Congratulations! Now you’ve learned about another data type known as a list. - A list is an ordered sequence of information - You can access an item in a list by using its index position - Yo…
  1. 1
    A list is an ordered set of objects in Python. Suppose we want to make a list of the heights of students in a class: - Jenny is 61 inches tall - Alexus is 70 inches tall - Sam is 67 inches tall …
  2. 2
    Lists can contain more than just numbers. Let’s revisit our height example: - Jenny is 61 inches tall - Alexus is 70 inches tall - Sam is 67 inches tall - Grace is 64 inches tall We can make a li…
  3. 3
    We’ve seen that the items in a list can be numbers or strings. They can also be other lists! Once more, let’s return to our class height example: - Jenny is 61 inches tall - Alexus is 70 inches t…
  4. 4
    Again, let’s return to our class height example: - Jenny is 61 inches tall - Alexus is 70 inches tall - Sam is 67 inches tall - Grace is 64 inches tall Suppose that we already had a list of names …
  5. 5
    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 later base…
  6. 6
    We can add a single element to a list using .append(). For example, suppose we have an empty list called empty_list: empty_list = [] We can add the element 1 using the following commands: empty…
  7. 7
    When we want to add multiple items to a list, we can use + to combine two lists. Below, we have a list of items sold at a bakery called items_sold: items_sold = [‘cake’, ‘cookie’, ‘bread’] Suppos…
  8. 8
    Often, we want to create a list of consecutive numbers. For example, suppose we want a list containing the numbers 0 through 9: my_list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] Typing out all of those nu…
  9. 9
    We can use range to generate more interesting lists. By default, range creates a list starting at 0. However, if we call range with two arguments, we can create a list that starts at a different …
  10. 10
    So far, we have learned - How to create a list - How to create a list of lists using zip - How to add elements to a list using either .append() or + - How to use range to create lists of integers …
  1. 1
    Now that we know how to create a list, we can start working with existing lists of data. In this lesson, you’ll learn how to: - Get the length of a list - Select subsets of a list (called _slicing…
  2. 2
    Often, we’ll need to find the number of items in a list, usually called its length. We can do this using the function len. When we apply len to a list, we get the number of elements in that lis…
  3. 3
    Chris is interviewing candidates for a job. He will call each candidate in order, represented by a Python list: calls = [‘Ali’, ‘Bob’, ‘Cam’, ‘Doug’, ‘Ellie’] First, he’ll call ‘Ali’, then ‘Bob’,…
  4. 4
    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…
  5. 5
    Suppose we have a list of letters: letters = [‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’] Suppose we want to select from b through f. We can do this using the following syntax: letters[start:end], where: …
  6. 6
    If we want to select the first 3 elements of a list, we could use the following code: >>> fruits = [‘apple’, ‘banana’, ‘cherry’, ‘date’] >>> print(fruits[0:3]) [‘apple’, ‘banana’, ‘cherry’] When s…
  7. 7
    Suppose we have a list called letters that represents the letters in the word “Mississippi”: letters = [‘m’, ‘i’, ‘s’, ‘s’, ‘i’, ‘s’, ‘s’, ‘i’, ‘p’, ‘p’, ‘i’] If we want to know how many times i …
  8. 8
    Sometimes, we want to sort a list in either numerical (1, 2, 3, …) or alphabetical (a, b, c, …) order. We can sort a list in place using .sort(). Suppose that we have a list of names: names …
  9. 9
    A second way of sorting a list is to use sorted. sorted is different from .sort() in several ways: 1. It comes before a list, instead of after. 2. It generates a new list. Let’s return to our l…
  10. 10
    In this lesson, we learned how to: - Get the length of a list - Select subsets of a list (called slicing) - Count the number of times that an element appears in a list - Sort a list of items