Key Concepts

Review core concepts you need to learn to master this subject

Python 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 in 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 to 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 based on the iterables passed in.

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 through and can be printed to visually represent the aggregated data. Empty iterables passed in will result in an empty iterator.

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.

Chevron Left Icon
Creating and Modifying a List in Python
Lesson 1 of 3
Chevron Right Icon
  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
  1. 1
    This lesson will help you review Python functions by providing some challenge exercises involving lists. As a refresher, function syntax looks like this: def some_function(some_input1, some_input…
  2. 2
    double_index(lst, index)
  3. 3
    remove_middle(lst, start, end)
  4. 4
    more_than_n(lst, item, n)
  5. 5
    more_frequent_item(lst, item1, item2)
  6. 6
    middle_element(lst)
  7. 7
    append_sum(lst)
  8. 8
    larger_list(lst1, lst2)
  9. 9
    combine_sort(lst1, lst2)
  10. 10
    append_size(lst)
  11. 11
    every_three_nums(start)

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