Dr. Roger Ianjamasimanana

Python list operations and properties

By Dr. Roger Ianjamasimanana

1. Definition and properties

Lists are like containers that hold multiple items, where each item has its own position. A Python list is a collection of items enclosed in square brackets [] and separated by commas. Here are some key properties of lists:

  • Ordered: the items in a list have a specific order (from left to right).
  • Mutable: you can change the list by adding, removing, or modifying elements.
  • Can contain elements of different data types: For example, a list can contain integers, strings, floats, and even other lists.
  • Allows duplicates: you can have the same value appear multiple times in a list.

2. Creating a Python list

To create a list in Python, you simply place comma-separated values inside square brackets.

3. Accessing elements in a list

Each item in a list has an index (starting from 0 for the first item, 1 for the second item, and so on). You can access individual elements by using their index. For instance, my_list[0] gives you the first element.

You can also use negative indexing to access elements from the end of the list. For example, my_list[-1] returns the last element.

4. Slicing a list

Slicing allows you to extract a portion of a list by specifying a range of indices. The syntax is list[start:end], which returns elements from start up to but not including end. You can also include a step value as list[start:end:step].

5. Python list built-in methods

Python provides several built-in methods to work with lists. Below are the most commonly used methods, along with explanations and examples.

5.1. append()

The append() method adds an element to the end of the list. You can use it when you need to add a single item to the list without affecting other elements. The append() method does not return anything.

Appending elements using a loop

5.2. extend()

This method extends a list by appending all the items from another iterable (e.g., another list).
It can be used when you want to merge multiple collections into a single list.

The syntax for extend() is:

list.extend(iterable)

Parameters:

  • iterable: any iterable (e.g., a list, tuple, string, or range) whose elements will be added to the list.

Returns:

The extend() method does not return anything.

5.3. insert()

This inserts an element at a specified index when you need to place a new element into a specific position rather than just appending it to the end. The basic syntax of insert is:

list.insert(index, element)

Parameters:

  • index: the position in the list where you want to insert the new element.
    • If the index is negative, the element is inserted counting from the end of the list.
    • If the index is greater than the list's length, the element is added at the end of the list.
  • element: the value you want to insert into the list.

Returns:

The method does not return anything; it modifies the list in place.

5.4. remove()

This method removes the first occurrence of a specified value from the list. You can use it when you know the exact value you want to remove, and you only want to remove its first instance.

The syntax for the remove() method in Python lists is:

list.remove(element)

Here, element is the value you want to remove from the list. The method does not return anything; it modifies the list in place by removing the first occurrence of the specified element.

5.5. pop()

The pop() method removes and returns the element at the specified index. If no index is specified, it removes and returns the last element. It is useful when you need to both remove an element from the list and use it elsewhere (e.g., storing it in a variable).

The syntax for pop() is:

list.pop(index)

Here index is optional; it specifies the position of the element you want to remove and return.

  • If the index is not specified, pop() removes and returns the last element in the list.
  • If the index is out of range, Python raises an IndexError.
  • If the index is negative, it counts from the end of the list (e.g., -1 is the last element).

The method returns the value of the element that was removed from the list.

5.6. index()

The index() method returns the index of the first occurrence of a specified value. You can use it when you need to locate where a particular value is in the list.

The syntax for index() is:

list.index(element, start, end)

Parameters:

  • element: the value you want to find the index of in the list. If the element is not found, Python raises a ValueError.
  • start (optional): the position in the list where the search should begin. the default is 0 (the start of the list).
  • end (optional): the position in the list where the search should end. The default is the length of the list (the end of the list).

Returns:

The method returns the index of the first occurrence of the specified element.

6. Other common list operations in Python

Checking if a list is decreasing in Python

To check if a list is decreasing, you can compare each element to the next one. If every element is greater than the following element, the list is strictly decreasing.

Getting a random number from a list in Python

Use the random.choice() function to select a random element from a list.

Removes all elements from the list

Sorting lists

To sort a list in ascending order, you can use the sort() method; for descending order you can use reverse=True for descending order.

Reverse the order

To reverse the order of a list, use the reverse method. This is equivalent to [::-1].

Copy a list

To copy the content of a list to another variable, use the copy method.

For nested lists, use deepcopy

Number of occurence of an item.

Let's find out how many times the number 2 occurs in the list below.

List concatenation

To combine the content of two or more lists into a new list, use the plus operator.

feature-top
Readers’ comment
feature-top
Log in to add a comment
🔐 Access