Python Lists

1. What is a Python list?

In Python, a list is a built-in data structure that allows you to store and manipulate a collection of items. It is an ordered sequence of elements enclosed in square brackets [ ] and separated by commas. Lists are versatile and can contain elements of different data types, such as integers, floats, strings, or even other lists.
In Python, lists are written in brackets:

Example

2. Access the elements of a list.

To access an element of a list, simply enter the name of the list followed by the index of the element concerned in square brackets:

Example

3. Modify the value of an item in the list

To change the value of a specific element, refer to the index number:

Example (Change the 3rd element of the list)




4. Length of a Python list

To determine the number of elements in a list (or length of the list), we use the len() method:

Example

5. Iterate through the elements of a Python list

The Python language has the for structure which allows you to browse any iterable (list, character string, etc.)

Example

You can also iterate through the elements of the list using indexes

Example

6. Add or remove items from the list

6.1 Add an item to a Python list

To add an item to the end of a python list, just use the append() method:

Example

To add an item at a specified index, just use the insert() method:

Example

6.2 Remove an item from a Python list

There are several methods to remove items from a list:

  1. remove(): removes a specified element.
  2. pop(): delete an element by specifying its index (or the last element if no index is specified)
  3. del: keyword deletes the element at the specified index ( del also completely deletes the list)
  4. clear(): empty the list

Example

Example

Example

The del keyword can also completely delete the list:

Example

Example (clear the list)

7. List comprehension

The list comprehension is an elegant and concise way to create a new list from an existing list in Python.
A list comprehension consists of an expression followed by a for statement in square brackets.
Here is an example of creating a list in formed comprehension of even numbers

Example

You can also add a condition to the for loop, for example if you want to get the list of even numbers that are multiple of 3:

Example




8. List slicing

We can perform a slicing to a range of items in a list using the slicing operator:

Example

9. Main methods associated with a python list

Python has a set of built-in methods for performing operations on lists:

  1. append(): Adds an item to the end of the list
  2. clear(): Remove all items from the list
  3. copy(): Returns a copy of the list
  4. count(): Returns the number of elements with the specified value
  5. extend(): Adds the elements of a list (or any iterable element) to the end of the current list
  6. index(): Returns the index of the first element with the specified value.
  7. insert(): Adds an element at the specified position
  8. pop(): Deletes the element at the specified position
  9. remove(): Removes the element with the specified value
  10. reverse(): Reverse the order of the list
  11. sort(): Sorts the list

10. Summary of characteristics and operations related to Python lists

Here's a description of some key characteristics and operations related to Python lists:

  1. Mutable: Lists are mutable, meaning you can modify their elements. You can change, add, or remove items from a list after it is created.
  2. Ordered: Lists maintain the order of the elements as they are inserted. The first element is at index 0, the second at index 1, and so on. You can access elements in a list by their index.
  3. Dynamic Size: Lists in Python can grow or shrink dynamically. You can add or remove items without needing to specify a fixed size in advance.
  4. Heterogeneous Elements: Lists can contain elements of different data types. For example, a single list can hold integers, strings, and floats together.
  5. Indexing and Slicing: You can access individual elements in a list using square brackets and the index of the item you want to retrieve. Negative indexing is also supported, allowing you to access elements from the end of the list. Additionally, you can use slicing to extract sublists by specifying start and end indices.
  6. Common Operations:
    1. Append: Adds an item to the end of the list using the append() method.
    2. Insert: Inserts an item at a specified position in the list using the insert() method.
    3. Remove: Removes the first occurrence of a specified item from the list using the remove() method.
    4. Length: Retrieves the number of items in the list using the len() function.
    5. Sort: Sorts the elements of the list in ascending order using the sort() method.
    6. Concatenate: Combines two lists together using the + operator.
    7. Iterate: You can iterate over the elements of a list using loops or list comprehensions.

 

Younes Derfoufi
CRMEF OUJDA

Leave a Reply