3.1 Lists

3.1 Lists

In Python, a list is a versatile and fundamental data structure that allows you to store and manipulate collections of items. Lists are ordered, mutable, and can contain elements of different data types. They are one of the most commonly used data structures in Python and are essential for many programming tasks.

Creating a List

To create a list in Python, you can use square brackets [] and separate the elements with commas. For example:

fruits = ['apple', 'banana', 'orange']

In this example, we have created a list called fruits that contains three strings: 'apple', 'banana', and 'orange'. Lists can also be empty, meaning they contain no elements:

empty_list = []

Accessing List Elements

You can access individual elements in a list by using their index. In Python, indexing starts at 0, so the first element of a list has an index of 0, the second element has an index of 1, and so on. For example:

fruits = ['apple', 'banana', 'orange']
print(fruits[0])  # Output: 'apple'
print(fruits[1])  # Output: 'banana'
print(fruits[2])  # Output: 'orange'

You can also use negative indexing to access elements from the end of the list. For example:

fruits = ['apple', 'banana', 'orange']
print(fruits[-1])  # Output: 'orange'
print(fruits[-2])  # Output: 'banana'
print(fruits[-3])  # Output: 'apple'

Modifying List Elements

Lists are mutable, which means you can change their elements after they are created. You can assign a new value to a specific index in the list to modify an element. For example:

fruits = ['apple', 'banana', 'orange']
fruits[1] = 'grape'
print(fruits)  # Output: ['apple', 'grape', 'orange']

You can also use slicing to modify multiple elements in a list at once. Slicing allows you to extract a portion of a list by specifying a start and end index. For example:

fruits = ['apple', 'banana', 'orange', 'grape', 'kiwi']
fruits[1:4] = ['cherry', 'mango', 'watermelon']
print(fruits)  # Output: ['apple', 'cherry', 'mango', 'watermelon', 'kiwi']

List Operations

Python provides several built-in operations and functions that you can use with lists.

Concatenation

You can concatenate two or more lists using the + operator. This creates a new list that contains all the elements from the original lists. For example:

list1 = [1, 2, 3]
list2 = [4, 5, 6]
concatenated_list = list1 + list2
print(concatenated_list)  # Output: [1, 2, 3, 4, 5, 6]

Repetition

You can repeat a list multiple times using the * operator. This creates a new list that contains the elements of the original list repeated the specified number of times. For example:

list1 = [1, 2, 3]
repeated_list = list1 * 3
print(repeated_list)  # Output: [1, 2, 3, 1, 2, 3, 1, 2, 3]

Length

You can determine the length of a list (i.e., the number of elements it contains) using the len() function. For example:

fruits = ['apple', 'banana', 'orange']
print(len(fruits))  # Output: 3

List Methods

Python provides a variety of built-in methods that you can use to manipulate lists. Here are some commonly used list methods:

append()

The append() method adds an element to the end of a list. For example:

fruits = ['apple', 'banana', 'orange']
fruits.append('grape')
print(fruits)  # Output: ['apple', 'banana', 'orange', 'grape']

insert()

The insert() method inserts an element at a specific index in a list. For example:

fruits = ['apple', 'banana', 'orange']
fruits.insert(1, 'grape')
print(fruits)  # Output: ['apple', 'grape', 'banana', 'orange']

remove()

The remove() method removes the first occurrence of a specified element from a list. For example:

fruits = ['apple', 'banana', 'orange']
fruits.remove('banana')
print(fruits)  # Output: ['apple', 'orange']

pop()

The pop() method removes and returns the element at a specific index in a list. If no index is specified, it removes and returns the last element. For example:

fruits = ['apple', 'banana', 'orange']
removed_fruit = fruits.pop(1)
print(removed_fruit)  # Output: 'banana'
print(fruits)  # Output: ['apple', 'orange']

sort()

The sort() method sorts the elements of a list in ascending order. For example:

numbers = [5, 2, 8, 1, 9]
numbers.sort()
print(numbers)  # Output: [1, 2, 5, 8, 9]

reverse()

The reverse() method reverses the order of the elements in a list. For example:

fruits = ['apple', 'banana', 'orange']
fruits.reverse()
print(fruits)  # Output: ['orange', 'banana', 'apple']

These are just a few of the many methods available for working with lists in Python. By understanding and utilizing these methods, you can efficiently manipulate and process data stored in lists.

In the next section, we will explore another important data structure in Python: tuples.