by BehindJava

What are Lists in Python with Examples

Home » python » What are Lists in Python with Examples

In this tutorial,we are going to learn about Lists in python.

Lists can be thought of the most general version of a sequence in Python. Unlike strings, they are mutable, meaning the elements inside a list can be changed! Lists are constructed with brackets [ ] and commas separating every element in the list.

Let’s go ahead and see how we can construct lists!

we will learn about:

  1. Creating lists
  2. Indexing and Slicing Lists
  3. Basic List Methods
  4. Nesting Lists
  5. Introduction to List Comprehensions

Creating lists

Assign a list to an variable, lists can actually hold different object types.

Example:

 x=[2,4,3]
y=[‘tom’,2,7.5]

Just like strings, the len() function will tell you how many items are in the sequence of the list.

Indexing and Slicing

Indexing and slicing work just like in strings. Let’s make a new list to remind ourselves of how this works:

my_list = ['one','two','three',4,5]
  1. Grab element at index 0.

    my_list[0]
  2. Grab index 1 and everything past it.

    my_list[1:]
  3. Grab everything UP TO index 3.

    my_list[:3]

Basic list Methods

Use the append method to permanently add an item to the end of a list:

  1. Append.

    list1.append('append me!')
  2. Use pop to “pop off” an item from the list. By default pop takes off the last index, but you can also specify which index to pop off.

Let’s see an example:

Pop off the 0 indexed item

 list1.pop(0)
  1. We can use the sort method and the reverse methods to also effect your lists:

    new_list = ['a','e','x','b','c']
    1. Use reverse to reverse order (this is permanent!).

      new_list.reverse()
    2. Use sort to sort the list (in this case alphabetical order, but for numbers it will go ascending)

      new_list.sort()

Nesting Lists

A great feature of of Python data structures is that they support nesting. This means we can have data structures within data structures. For example: A list inside a list. Example:

Let’s make three lists.

lst_1=[1,2,3]
lst_2=[4,5,6]
lst_3=[7,8,9]
  1. Make a list of lists to form a matrix

    matrix = [lst_1,lst_2,lst_3]
  2. We can again use indexing to grab elements, but now there are two levels for the index. The items in the matrix object, and then the items inside that list!

    matrix[0]-lst_1
  3. Grab first item of the first item in the matrix object

    matrix[0][0]

List Comprehensions

Python has an advanced feature called list comprehensions. They allow for quick construction of lists.

To fully understand list comprehensions we need to understand for loops. So don’t worry if you don’t completely understand this section, and feel free to just skip it since we will return to this topic later.

Here are a few examples!

  1. Build a list comprehension by deconstructing a for loop within a []

    first_col = [row[0] for row in matrix]

    We used a list comprehension here to grab the first element of every row in the matrix.