Python Lists

A list in Python is an ordered collection of items that can store multiple elements. Lists are mutable, meaning their elements can be changed after the list is created. Lists can store elements of any data type, including numbers, strings, and even other lists.

What is a List?

A list in Python is defined by enclosing the elements in square brackets [], separated by commas. Lists are ordered, changeable, and allow duplicate values.

Syntax


# Creating a list in Python
my_list = [1, 2, 3, 4, 5]  # A list of integers
string_list = ["apple", "banana", "cherry"]  # A list of strings
mixed_list = [1, "apple", 3.14, True]  # A list with mixed data types
            

Explanation:

In the above example:

Accessing Elements in a List

Elements in a list can be accessed using indexing. The index starts at 0 for the first element and goes up to n-1 for the nth element. Negative indices can be used to access elements from the end of the list.

Accessing elements in a list


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

# Access the first element
first_element = my_list[0]  # Index 0 corresponds to the first element
print("First element:", first_element)

# Access the last element
last_element = my_list[-1]  # Index -1 corresponds to the last element
print("Last element:", last_element)

# Access elements from index 1 to 2 (slice from index 1 to 3, but excluding 3)
substring = my_list[1:3]  
print("Substring (elements from index 1 to 2):", substring)
                
            

Output

First element: 1 Last element: 5 Substring (elements from index 1 to 2): [2, 3]

Explanation:

In this example:

Modifying Lists

Since lists are mutable, you can modify their elements by directly assigning new values to specific indices. You can also add or remove elements from a list.

Modifying an Element


# Modifying elements in a list
my_list = [1, 2, 3, 4, 5]

# Modify the third element (index 2) to 10
my_list[2] = 10

# Print the updated list
print("Updated list:", my_list)
          

Output

Updated list: [1, 2, 10, 4, 5]

Explanation:

Here, my_list[2] = 10 changes the third element from 3 to 10. The updated list will be [1, 2, 10, 4, 5].

Adding Elements to a List


my_list = [1, 2, 3, 4]

# Add 5 to the end of the list
my_list.append(5)
print("After append:", my_list)

# Insert 10 at index 2
my_list.insert(2, 10)
print("After insert:", my_list)
            

Output

After append: [1, 2, 3, 4, 5] After insert: [1, 2, 10, 3, 4, 5]

Explanation:

In the above example:

Removing Elements from a List


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

# Remove the first occurrence of 3
my_list.remove(3)
print("After remove(3):", my_list)

# Remove the element at index 1 (which is 2)
my_list.pop(1)
print("After pop(1):", my_list)
            

Output

After remove(3): [1, 2, 4, 5] After pop(1): [1, 4, 5]

Explanation:

Here:

List Methods

Python provides several built-in methods to perform common operations on lists. These methods allow us to manipulate and interact with lists more efficiently.

Examples:

List Methods


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

# Get the length of the list
length = len(my_list)
print("Length of the list:", length)

# Sort the list in ascending order
my_list.sort()  # Modifies the list in place
print("After sort:", my_list)

# Reverse the order of elements
my_list.reverse()  # Modifies the list in place
print("After reverse:", my_list)

# Count how many times 1 appears in the list
count_ones = my_list.count(1)
print("Count of 1 in the list:", count_ones)
            

Output

Length of the list: 5 After sort: [1, 2, 3, 4, 5] After reverse: [5, 4, 3, 2, 1] Count of 1 in the list: 1

Explanation:

In the example:

Nested Lists

Python lists can contain other lists as elements. These are known as nested lists. You can access elements in nested lists using multiple indices.

Example


# Nested lists
nested_list = [[1, 2], [3, 4], [5, 6]]
# Access the first sublist
first_sublist = nested_list[0]
print("First sublist:", first_sublist)

# Access the second element of the first sublist
first_element_of_sublist = nested_list[0][1]
print("Second element of the first sublist:", first_element_of_sublist)
            

Output

First sublist: [1, 2] Second element of the first sublist: 2

Explanation:

In the example: