Python Lists

In Python, a list is a collection that contains a sequence of items and can be mutable. Lists are one of the most commonly used data structures in Python because they allow you to store and manipulate enumerable values.

Creating a List

To make a list, you have to put in a comma-separated array of items that are categorized into numerical symbols, surrounded by a punctuation mark.

Here’s an example:

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

Accessing List Items

You can access them using the special items in the list, which use their index, which starts at 0.

For example:

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

Modifying List Items

You can modify the list by adding new values at particular indexes to them.

For example:

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

List Length

To determine the length of a list (i.e. the number of items it contains), you can use the len() function.

For example:

print(len(fruits))  # Output: 3

Adding Items to a List

You can add items to the end of the list using the append() method or insert items at particular indexes using the insert() method.

Here are some examples:

print(fruits)  # Output: ['apple', 'grape', 'orange', 'mango']

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

Removing Items from a List

You can remove items from the list using the remove() method where you specify the value you want to remove, or you can use the del method to remove items at particular indexes.

Here are some examples:

print(fruits)  # Output: ['kiwi', 'grape', 'orange', 'mango']

del fruits[1]
print(fruits)  # Output: ['kiwi', 'orange', 'mango']

List Slicing

You can extract a part of the list using slicing. Using slicing you can specify a range of indexes to remove.

For example:

print(fruits[1:3])  # Output: ['orange', 'mango']
print(fruits[:2])   # Output: ['kiwi', 'orange']
print(fruits[2:])   # Output: ['mango']

There are many other operations with lists, such as sorting, reversing, or mutating them. There are several ways in which elements can be managed in a list, which is a fundamental data structure in Python.

Example Combination of Lists

# Creating a list of student names
students = ['Alice', 'Bob', 'Charlie', 'David', 'Eve']

# Accessing and modifying list items
print(students[0])  # Output: Alice

students[1] = 'Bobby'
print(students)  # Output: ['Alice', 'Bobby', 'Charlie', 'David', 'Eve']

# List length
num_students = len(students)
print("Number of students:", num_students)  # Output: Number of students: 5

# Adding items to a list
students.insert(2, 'Cindy')
print(students)  # Output: ['Alice', 'Bobby', 'Cindy', 'Charlie', 'David', 'Eve', 'Frank']

# Removing items from a list
del students[3]
print(students)  # Output: ['Alice', 'Bobby', 'Cindy', 'Eve', 'Frank']

# List slicing
print(students[1:4])  # Output: ['Bobby', 'Cindy', 'Eve']
print(students[:3])   # Output: ['Alice', 'Bobby', 'Cindy']
print(students[3:])   # Output: ['Eve', 'Frank']

# Combining multiple lists
grades = [85, 92, 78, 89, 95]
combined_list = list(zip(students, grades))
print(combined_list)  # Output: [('Alice', 85), ('Bobby', 92), ('Cindy', 78), ('Eve', 89), ('Frank', 95)]

# List comprehension
squared_numbers = [num**2 for num in range(1, 6)]
print(squared_numbers)  # Output: [1, 4, 9, 16, 25]

# Filtering with list comprehension
even_numbers = [num for num in range(1, 11) if num % 2 == 0]
print(even_numbers)  # Output: [2, 4, 6, 8, 10]

# Checking if an item exists in a list
if 'Alice' in students:
    print("Alice is in the list")  # Output: Alice is in the list

# Sorting a list
print(students)  # Output: ['Alice', 'Bobby', 'Cindy', 'Eve', 'Frank']

# Reversing a list
print(students)  # Output: ['Frank', 'Eve', 'Cindy', 'Bobby', 'Alice']


['Alice', 'Bobby', 'Charlie', 'David', 'Eve']
Number of students: 5
['Alice', 'Bobby', 'Cindy', 'Charlie', 'David', 'Eve', 'Frank']
['Alice', 'Bobby', 'Cindy', 'Eve', 'Frank']
['Bobby', 'Cindy', 'Eve']
['Alice', 'Bobby', 'Cindy']
['Eve', 'Frank']
[('Alice', 85), ('Bobby', 92), ('Cindy', 78), ('Eve', 89), ('Frank', 95)]
[1, 4, 9, 16, 25]
[2, 4, 6, 8, 10]
Alice is in the list
['Alice', 'Bobby', 'Cindy', 'Eve', 'Frank']
['Frank', 'Eve', 'Cindy', 'Bobby', 'Alice']