Lists in Python are ordered, mutable, and allow duplicate values. They are one of the most commonly used data structures, providing flexibility to store different types of elements like integers, floats, strings, and even other lists.


Creating a List

A list can be created using square brackets [] or the list() constructor.

# Empty list
my_list = []

# List with elements
numbers = [1, 2, 3, 4, 5]

# List with different data types
mixed_list = [1, "Hello", 3.14, True]

# Using list constructor
constructed_list = list((10, 20, 30))  # tuple converted to list

Accessing Elements in a List

Elements in a list can be accessed using indexing and slicing.

numbers = [10, 20, 30, 40, 50]

print(numbers[0])  # 10 (first element)
print(numbers[-1]) # 50 (last element)
print(numbers[1:4]) # [20, 30, 40] (slice from index 1 to 3)

Modifying a List (Insert, Update, Remove)

Inserting Elements

Python provides multiple ways to insert elements:

  • append() Adds an element at the end;
  • insert(index, value) Inserts at a specific position;
  • extend(iterable) Adds multiple elements;
my_list = [1, 2, 3]

# Append
my_list.append(4)
print(my_list)  # [1, 2, 3, 4]

# Insert at index 1
my_list.insert(1, 10)
print(my_list)  # [1, 10, 2, 3, 4]

# Extend (add multiple elements)
my_list.extend([5, 6, 7])
print(my_list)  # [1, 10, 2, 3, 4, 5, 6, 7]

Updating Elements

Simply assign a new value using indexing.

my_list = [1, 2, 3, 4]
my_list[1] = 20  # Update index 1
print(my_list)  # [1, 20, 3, 4]

Removing Elements

Python provides multiple ways to remove elements:

  • remove(value) Removes the first occurrence of a value;
  • pop(index) Removes and returns an element at the specified index;
  • del list[index] Deletes an element by index;
  • clear() Removes all elements;
my_list = [10, 20, 30, 40, 50]

# Remove by value
my_list.remove(30)
print(my_list)  # [10, 20, 40, 50]

# Remove by index
removed_element = my_list.pop(1)  # Removes element at index 1
print(my_list)  # [10, 40, 50]
print(removed_element)  # 20

# Delete using del
del my_list[0]  # Deletes first element
print(my_list)  # [40, 50]

# Clear the entire list
my_list.clear()
print(my_list)  # []

Sorting Lists

Python provides multiple ways to sort lists.

Using sort() (Modifies the Original List)

numbers = [5, 2, 9, 1, 5, 6]
numbers.sort()
print(numbers)  # [1, 2, 5, 5, 6, 9]

- Reverse Order

numbers = [5, 2, 9, 1, 5, 6]
numbers.sort(reverse=True)
print(numbers)  # [9, 6, 5, 5, 2, 1]

- Sort with Key Function

words = ["apple", "banana", "kiwi", "cherry"]
words.sort(key=len)
print(words)  # ['kiwi', 'apple', 'cherry', 'banana']

Using sorted() (Returns a New List)

numbers = [5, 2, 9, 1, 5, 6]
sorted_numbers = sorted(numbers)
print(sorted_numbers)  # [1, 2, 5, 5, 6, 9]
print(numbers)  # [5, 2, 9, 1, 5, 6] Original list remains unchanged

Iterating Over a List

- Using a for Loop

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

for fruit in fruits:
    print(fruit)

Using enumerate() (Index and Value)

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

for index, fruit in enumerate(fruits):
    print(f"Index {index}: {fruit}")

'''
Index 0: apple
Index 1: banana
Index 2: cherry
'''

Using List Comprehension

numbers = [1, 2, 3, 4, 5]
squared = [x**2 for x in numbers]
print(squared)  # [1, 4, 9, 16, 25]

Example with index = -1

Using index -1 allows access to the last element of the list.

my_list = [10, 20, 30, 40, 50]

print(my_list[-1])  # 50 (Last element)
print(my_list[-2])  # 40 (Second last element)

Copying a List

Using Slicing

original = [1, 2, 3]
copy_list = original[:]
print(copy_list) # [1, 2, 3]

Using copy()

original = [1, 2, 3]
copy_list = original.copy()
print(copy_list) # [1, 2, 3]

Using list() Constructor

original = [1, 2, 3]
copy_list = list(original)
print(copy_list) # [1, 2, 3]

Using deepcopy() for Nested Lists

import copy

nested_list = [[1, 2], [3, 4]]
deep_copy = copy.deepcopy(nested_list)
print(deep_copy) # [[1, 2], [3, 4]]

Checking if an Item Exists in a List

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

if "banana" in fruits:
    print("Banana is in the list")

List Length

numbers = [1, 2, 3, 4]
print(len(numbers))  # 4

Reversing a List

Using reverse() (Modifies Original List)

numbers = [1, 2, 3, 4]
numbers.reverse()
print(numbers)  # [4, 3, 2, 1]

Using Slicing

numbers = [1, 2, 3, 4]
reversed_list = numbers[::-1]
print(reversed_list)  # [4, 3, 2, 1]

Python lists are powerful, flexible, and widely used. They provide easy methods to insert, update, delete, sort, iterate, and manipulate elements.