Skip to main content

Introduction to Python Lists

Python Lists

A list is a collection of items enclosed in square brackets [], and each item in the list is separated by a comma.

Lists are mutable, meaning that you can change their content by adding, removing, or modifying elements.

Lists can contain elements of different data types, such as numbers, strings, or even other lists.

Here's an example of a list in Python:

fruits = ["apple", "banana", "orange", "kiwi"]

Accessing List Elements

You can access individual elements of a list by using their index, starting from 0 for the first element. You can also use negative indices to access elements from the end of the list.

As an example:

fruits = ["apple", "banana", "orange", "kiwi"]

print(fruits[0]) # Output: "apple"
print(fruits[-1]) # Output: "kiwi"

Modifying List Elements

Lists are mutable, so you can modify elements by assigning new values to specific indices.

fruits = ["apple", "banana", "orange", "kiwi"]

fruits[1] = "grape" # Modifying the second element
print(fruits) # Output: ["apple", "grape", "orange", "kiwi"]

List Slicing

You can extract a sublist (slice) from a list using the slicing operator :. Slicing allows you to specify a start index, an end index (exclusive), and an optional step size.

As an example:

fruits = ["apple", "banana", "orange", "kiwi", "grape", "melon"]

print(fruits[1:4]) # Output: ["banana", "orange", "kiwi"]
print(fruits[:3]) # Output: ["apple", "banana", "orange"]
print(fruits[2:]) # Output: ["orange", "kiwi", "grape", "melon"]
print(fruits[1:6:2]) # Output: ["banana", "kiwi", "melon"]

List Methods

Python provides built-in methods to manipulate lists. Some commonly used methods include append(), extend(), insert(), remove(), pop(), index(), count(), sort(), and reverse().

As an example:

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

fruits.append("kiwi") # Add an element at the end of the list
print(fruits) # Output: ["apple", "banana", "orange", "kiwi"]

fruits.insert(1, "grape") # Insert an element at a specific index
print(fruits) # Output: ["apple", "grape", "banana", "orange", "kiwi"]

fruits.remove("banana") # Remove an element from the list
print(fruits) # Output: ["apple", "grape", "orange", "kiwi"]

fruits.pop(2) # Remove and return an element at a specific index
print(fruits) # Output: ["apple", "grape", "kiwi"]

fruits.sort() # Sort the elements in ascending order
print(fruits) # Output: ["apple", "grape", "kiwi"]

List Length

You can get the length of a list (the number of elements in the list) using the len() function.

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

print(len(fruits)) # Output: 3

List Concatenation

You can concatenate two or more lists using the + operator. This creates a new list containing all the elements from the operands.

fruits = ["apple", "banana"]
more_fruits = ["orange", "kiwi"]

all_fruits = fruits + more_fruits
print(all_fruits) # Output: ["apple", "banana", "orange", "kiwi"]

List Repetition

You can repeat a list by using the * operator. This creates a new list with multiple copies of the original list.

fruits = ["apple", "banana"]

repeated_fruits = fruits * 3
print(repeated_fruits) # Output: ["apple", "banana", "apple", "banana", "apple", "banana"]

Checking List Membership

You can check if an element exists in a list using the in operator. It returns True if the element is found in the list and False otherwise.

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

print("banana" in fruits) # Output: True
print("kiwi" in fruits) # Output: False

Copying a List

There are a few ways to create a copy of a list. The simplest way is to use the slicing operator [:], which creates a shallow copy of the list.

fruits = ["apple", "banana", "orange"]
copy_of_fruits = fruits[:]

print(copy_of_fruits) # Output: ["apple", "banana", "orange"]

List Iteration

You can iterate over the elements of a list using a loop, such as for or while. This allows you to perform operations on each element of the list.

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

for fruit in fruits:
print(fruit)

# Output:
# apple
# banana
# orange

List Comprehension

List comprehension is a concise way to create lists based on existing lists. It allows you to apply an expression to each element in the original list and generate a new list.

As an example:

numbers = [1, 2, 3, 4, 5]

squared_numbers = [x**2 for x in numbers]
print(squared_numbers) # Output: [1, 4, 9, 16, 25]