Learn Python: Lists

Learn about lists, a data structure in Python used to store ordered groups of data.

Start[missing "en.views.course_landing_page.learn-python-3.course_illustration" translation]

Key Concepts

Review core concepts you need to learn to master this subject

Python Lists

number_list = [1, 2, 3, 4, 5, 6, 7, 10] print(number_list) empty_list = []

In Python, lists are ordered collections of items that allow for easy use of a set of data. Lists can have duplicate values.

List values are placed in between square brackets [ ], separated by commas, and it is good practice to put a space in between the comma and the next value. Empty lists do not contain any values within the square brackets.

Python Lists: Data Types

number_list = [1, 2, 3, 4, 5, 6, 7, 10] print(number_list) empty_list = []

In Python, lists are considered the most versatile datatype available because a single list can contain multiple different data types within the same square brackets. These include numbers, strings, other objects, and even other lists. Lists containing different datatypes are still created using square brackets "[ ]" with a comma separated list inside.

Python: Aggregating Iterables Using zip()

number_list = [1, 2, 3, 4, 5, 6, 7, 10] print(number_list) empty_list = []

In Python, datatypes 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 above, 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.

Python: Adding Items to Lists Using .append()

number_list = [1, 2, 3, 4, 5, 6, 7, 10] print(number_list) 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.

Python: Adding Lists Together

number_list = [1, 2, 3, 4, 5, 6, 7, 10] print(number_list) 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. An important thing to remember is that 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.

Determining List Length with len()

number_list = [1, 2, 3, 4, 5, 6, 7, 10] print(number_list) empty_list = []

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

Python Zero-Indexing

number_list = [1, 2, 3, 4, 5, 6, 7, 10] print(number_list) empty_list = []

An item's position in a list is represented by its index. In Python, list indexing begins at zero, and ends at the length of the list minus one. For example, in this list, 'Andy' is found at index 2.

Python List Indices

number_list = [1, 2, 3, 4, 5, 6, 7, 10] print(number_list) 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.

Indices can also be used as negative numbers, referring to the placement from the end of a list. The last element of a list is index -1, the second-to-last is -2 and so on.

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

Python List Slicing

number_list = [1, 2, 3, 4, 5, 6, 7, 10] print(number_list) 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.

Python list item ranges including first or last item

number_list = [1, 2, 3, 4, 5, 6, 7, 10] print(number_list) 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 :.

Python Negative List Indices

number_list = [1, 2, 3, 4, 5, 6, 7, 10] print(number_list) 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 three elements of a list: my_list[-3:]. To select everything except the last two elements:my_list[:-2]`

The .count() List Method

number_list = [1, 2, 3, 4, 5, 6, 7, 10] print(number_list) 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.

The .sort() List Method

number_list = [1, 2, 3, 4, 5, 6, 7, 10] print(number_list) 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.

The Python sorted() Function

number_list = [1, 2, 3, 4, 5, 6, 7, 10] print(number_list) 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: [...] Why would we create an empty list? Usually, it's because we're planning on filling it later based on some...

  6. 6

    We can add a single element to a list using [...] . For example, suppose we have an empty list called [...] : [...] We can add the element [...] using the following commands: [...] If w...

  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 [...] : [...] Suppose the bakery wants to start sel...

  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: [...] Typing out all of those numbers takes time and the more numb...

  9. 9

    We can use [...] to generate more interesting lists. By default, [...] creates a list starting at [...] . However, if we call [...] with two arguments, we can create a list that starts at ...

  10. 10

    So far, we have learned - How to create a list - How to create a list of lists using [...] - How to add elements to a list using either [...] or [...] - How to use [...] to create lists of ...

  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 [...] . When we apply [...] to a list, we get the number of elements in ...

  3. 3

    Chris is interviewing candidates for a job. He will call each candidate in order, represented by a Python list: [...] First, he'll call [...] , then [...] , etc. In Python, we call the order ...

  4. 4

    What if we want to select the last element of a list? We can use the index [...] to select the last item of a list, even when we don't know how many elements are in a list. Consider the followi...

  5. 5

    Suppose we have a list of letters: [...] Suppose we want to select from [...] through [...] . We can do this using the following syntax: [...] , where: - [...] is the index of the first el...

  6. 6

    If we want to select the first 3 elements of a list, we could use the following code: [...] When starting at the beginning of the list, it is also valid to omit the [...] : [...] We can do som...

  7. 7

    Suppose we have a list called [...] that represents the letters in the word "Mississippi": [...] If we want to know how many times [...] appears in this word, we can use the function [...] ...

  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 [...] . Suppose that we have a list of names: [...]...

  9. 9

    A second way of sorting a list is to use [...] . [...] is different from [...] in several ways: 1. It comes before a list, instead of after. 2. It generates a new list. Let's return to our...

  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: [...] For example, a function that ret...

  2. 2

    [...]

  3. 3

    [...]

  4. 4

    [...]

  5. 5

    [...]

  6. 6

    [...]

  7. 7

    [...]

  8. 8

    [...]

  9. 9

    [...]

  10. 10

    [...]

  11. 11

    [...]

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

Learn Python: Lists

Start[missing "en.views.course_landing_page.learn-python-3.course_illustration" translation]