1. What is a Python list?

A Python list is an ordered and editable collection of data objects. Unlike an array, which can contain objects of a single type, a list can contain a mixture of objects.
A list in Python is a data type that is part of collections, ordered and modifiable. In Python, lists are written in brackets:

List = [ ...]

Example

#Creating a list
myList = ["Python", "Django", "Laravel"]
# Displaying the list
print(myList) # output: ['Python', 'Django', 'Laravel']

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

Print the 2nd element of the list:
myList = ["Python", "Java", "PHP"]
print(myList[1]) # output 'Java'

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)

myList = ["Python", "Java", "PHP"]
myList[2]="NodeJS"
print(myList) #output: ['Python','Java','NodeJS']

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

#Display the number of elements in the list:
myList = ["Python", "Django", "PyGame"]
print("The length of the list is",len(myList)) 
#output: The length of the list is 3

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

#Print all elements of the list, one by one:
myList = ["Python", "Java", "NodeJS"]
for element in myList:
     print(element) # Print all list elements one by one.

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

Example

#Print all elements of the list, one by one:
myList = ["Javascript", "PHP", "NodeJS"]
# getting the length of list
n = len(myList)
for i in range(0, n):
     print(myList[i]) # Print all list items one by one.

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

#add an element at the end of the list with the append() method:
myList = ["Python", "Java", "PHP"]
myList.append("Django")
print(myList) #output: ["Python", "Java", "PHP", "Django"]

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

Example

#Insert an element in second position:
myList = ["Python", "Java", "PHP"]
myList. insert(1, "PyGame")
print(myList) # output: ["Python", "PyGame", "Java", "PHP"]

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

#remove a specified element with the remove() method
myList = ["Python", "C++", "PHP"]
myList.remove("PHP")
print(myList) #output: ["Python", "C++"]

Example

#Remove specified index item with pop() method
myList = ["Python", "Java", "PHP"]
myList.pop(1)
print(myList) #Displays: ["Python", "PHP"]

Example

#delete element at a specified index with the del method:
myList = ["Python", "Java", "PHP"]
del myList[2]
print(myList) #Displays: ["Python", "Java"]

The del keyword can also completely delete the list:

Example

myList = ["Python", "Django", "PyGame"]
del myList
print(myList) #this will cause an error because "myList" no longer exists.

Example (clear the list)

myList = ["Python", "Django", "Flask"]
myList. clear()
print(myList) #this an empty 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

list_even = [2*n for n in range(5)]
print("List of even numbers: ", list_even)
# displays: List of even numbers: [0, 2, 4, 6, 8]

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

list_even_mult_3 = [2*n for n in range(20) if n%3 == 0]
print("even numbers multiple of 3: ", list_even_mult_3)
# displays: even numbers multiple of 3: [0, 6, 12, 18, 24, 30, 36]

8. List slicing

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

Example

# List slicing in Python
my_list = ['h','e','l','l','o','w','o','r','l', 'd']

# split from index 3 to index 6
print(my_list[3:6]) # prints: ['l', 'o', 'w']

# slicing from index 4 to end
print(my_list[4:]) # prints: ['o', 'w', 'o', 'r', 'l', 'd']

# slicing from start up to index 5 not included
print(my_list[:5]) # prints: ['h','e','l','l','o']

# slicing from start to end
print(my_list[:])# prints: ['h', 'e', ​​'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd' ]

# extract the last element
print(my_list[-1])# displays: [d]

# extract element with index 4 counting from last element
print(my_list[-4]) # prints: o

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

 

Younes Derfoufi
my-courses.net

Leave a Reply