It is a collection of homogenous and heterogeneous elements. It is a versatile and widely-used data type in Python. Lists are written with square brackets '[ ]' .
Lists can be created by placing a comma-separated sequence of elements within square brackets.
# Empty list
empty_list = []
# List of integers
int_list = [1, 2, 3, 4, 5]
# List of strings
string_list = ["apple", "banana", "cherry"]
# List of mixed data types
mixed_list = [1, "apple", 3.14, True]
Elements in a list are accessed by indexing, with the first element at index 0. Negative indexing is also supported, with -1 being the last element.
Examples:
fruits = ["apple", "banana", "cherry"]
print(fruits[0]) # Output: apple
print(fruits[1]) # Output: banana
print(fruits[-1]) # Output: cherry
Since lists are mutable, you can change their elements by accessing the index and assigning a new value.
Examples:
fruits = ["apple", "banana", "cherry"]
fruits[1] = "blueberry"
print(fruits) # Output: ['apple', 'blueberry', 'cherry']
You can extract a portion of a list using slicing, which creates a new list containing the desired elements.
Examples:
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(numbers[2:5]) # Output: [2, 3, 4]
print(numbers[:3]) # Output: [0, 1, 2]
print(numbers[5:]) # Output: [5, 6, 7, 8, 9]
print(numbers[-3:]) # Output: [7, 8, 9]
print(numbers[::2]) # Output: [0, 2, 4, 6, 8] (step slicing)
You can add elements to a list using methods like append(), insert(), and extend().
Examples:
fruits = ["apple", "banana", "cherry"]
# Append adds an element to the end
fruits.append("orange")
print(fruits) # Output: ['apple', 'banana', 'cherry', 'orange']
# Insert adds an element at a specified position
fruits.insert(1, "blueberry")
print(fruits) # Output: ['apple', 'blueberry', 'banana', 'cherry', 'orange']
# Extend adds multiple elements to the end
more_fruits = ["kiwi", "mango"]
fruits.extend(more_fruits)
print(fruits) # Output: ['apple', 'blueberry', 'banana', 'cherry', 'orange', 'kiwi', 'mango']
You can remove elements from a list using methods like remove(), pop(), and del.
Examples:
fruits = ["apple", "banana", "cherry", "orange", "kiwi", "mango"]
# Remove removes the first occurrence of a specified value
fruits.remove("banana")
print(fruits) # Output: ['apple', 'cherry', 'orange', 'kiwi', 'mango']
# Pop removes and returns the element at a specified position (default is the last element)
removed_fruit = fruits.pop(2)
print(removed_fruit) # Output: orange
print(fruits) # Output: ['apple', 'cherry', 'kiwi', 'mango']
# Del removes elements by index
del fruits[1]
print(fruits) # Output: ['apple', 'kiwi', 'mango']
Examples:
numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5]
# Sorting the list
numbers.sort()
print(numbers) # Output: [1, 1, 2, 3, 4, 5, 5, 6, 9]
# Reversing the list
numbers.reverse()
print(numbers) # Output: [9, 6, 5, 5, 4, 3, 2, 1, 1]
# Finding the index of an element
print(numbers.index(5)) # Output: 2
# Counting occurrences of an element
print(numbers.count(1)) # Output: 2
# Clearing the list
numbers.clear()
print(numbers) # Output: []
# Copying a list
numbers = [1, 2, 3]
numbers_copy = numbers.copy()
print(numbers_copy) # Output: [1, 2, 3]