A list in Python is an ordered collection of elements that can store different data types. Lists are defined using square brackets []
, with items separated by commas.
my_list = ["Dublin", 123, True, 3.14, "Test"]
print(my_list[0]) # Dublin
print(my_list[1]) # 123
print(my_list[-1]) # Test
Lists are mutable, meaning you can modify their elements after creation. However, trying to access an index that doesn’t exist will raise an IndexError
. The index must be an integer, as using a float or other types will result in a TypeError
.
You can also create a list from any iterable, such as tuples and sets.
tuple_1 = (1, 2, 3)
set_1 = {4, 5, 6}
a = list(tuple_1)
b = list(set_1)
print(type(a)) # <class 'list'>
print(type(b)) # <class 'list'>
Slicing a List
Slicing allows extracting a portion of a list to create a new list. This is done using the [start:stop]
syntax, where start
is inclusive, and stop
is exclusive.
my_list = ["Dublin", 123, True, 3.14, "Test"]
print(my_list[1:3]) # [123, True]
print(my_list[2:]) # [True, 3.14, 'Test']
print(my_list[:2]) # ['Dublin', 123]
# Modify a slice
my_list[0:2] = ["London", "Dublin"]
print(my_list) # ['London', 'Dublin', True, 3.14, 'Test']
You can also use slicing with a step value [start:stop:step]
to skip elements.
numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print(numbers[::2]) # [0, 2, 4, 6, 8] (Every second element)
print(numbers[::-1]) # [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] (Reverse list)
Concatenating Lists
Lists can be combined using the +
operator.
ireland = ["Dublin", "Cork", "Galway"]
england = ["London", "Manchester"]
all_cities = ireland + england
print(all_cities) # ['Dublin', 'Cork', 'Galway', 'London', 'Manchester']
Alternatively, you can use .extend()
to modify a list in place.
ireland.extend(england)
print(ireland) # ['Dublin', 'Cork', 'Galway', 'London', 'Manchester']
Deleting List Elements
The del
statement allows removing elements or entire slices.
cities = ["Dublin", "Cork", "Waterford"]
del cities[1]
print(cities) # ['Dublin', 'Waterford']
# Delete a slice
del cities[:]
print(cities) # []
To remove an item by value instead of index, use .remove()
.
fruits = ["apple", "banana", "cherry"]
fruits.remove("banana")
print(fruits) # ['apple', 'cherry']
Iterating a List
Python provides multiple ways to iterate over lists efficiently.
Using for ... in
cities = ["Dublin", "Cork", "Waterford"]
for city in cities:
print(city)
Using range()
and len()
for i in range(len(cities)):
print(f"Index {i}: {cities[i]}")
Using enumerate()
for index, city in enumerate(cities, start=1):
print(f"City {index}: {city}")
Iterating Backwards
for city in reversed(cities):
print(city)
Iterating Multiple Lists
cities = ["Tokyo", "Dublin", "Paris"]
countries = ["Japan", "Ireland", "France"]
for city, country in zip(cities, countries):
print(f"{city} is in {country}")
Finding an Item with next()
The next()
function allows retrieving the first matching element efficiently.
nums = [1, 2, 3, 4, "Dublin", "foo"]
find = lambda item: next((i for i in nums if i == item), None)
print(find(3)) # 3
print(find("xxx")) # None
Commonly Used List Methods
append(x)
Adds an item to the end of the list.
fruits = ["apple", "banana"]
fruits.append("cherry")
print(fruits) # ['apple', 'banana', 'cherry']
clear()
Removes all elements from the list.
fruits.clear()
print(fruits) # []
copy()
Creates a shallow copy of the list.
original = [1, 2, 3]
copy_list = original.copy()
print(copy_list) # [1, 2, 3]
count(x)
Returns the number of times x
appears in the list.
nums = [1, 2, 3, 2, 2, 4]
print(nums.count(2)) # 3
index(x[, start[, end]])
Returns the index of the first occurrence of x
.
print(nums.index(3)) # 2
insert(i, x)
Inserts x
at index i
.
nums.insert(2, 99)
print(nums) # [1, 2, 99, 3, 2, 2, 4]
pop([i])
Removes and returns the item at index i
.
print(nums.pop()) # Removes last element
print(nums.pop(2)) # Removes element at index 2
remove(x)
Removes the first occurrence of x
.
nums.remove(2)
print(nums) # First `2` is removed
reverse()
Reverses the list in place.
nums.reverse()
print(nums)
sort(key=None, reverse=False)
Sorts the list.
nums.sort()
print(nums)
Commonly Used List Functions
max(iterable, *, key=None)
Returns the largest item.
print(max([1, 5, 3, 9, 2])) # 9
min(iterable, *, key=None)
Returns the smallest item.
print(min([1, 5, 3, 9, 2])) # 1
sum(iterable, /, start=0)
Returns the sum of all items.
print(sum([1, 2, 3, 4])) # 10
map():
Transforming Lists
The map()
function applies a given function to each element in a list and returns an iterator with the transformed values.
numbers = [1, 2, 3, 4, 5]
# Multiply each number by 2
doubled = list(map(lambda x: x * 2, numbers))
print(doubled) # [2, 4, 6, 8, 10]
Another common use case is converting types:
string_numbers = ["1", "2", "3", "4"]
int_numbers = list(map(int, string_numbers))
for i in int_numbers:
print(type(i))
map()
function is a good choice to process each element in a list and transform them into different values.
names = ["jack sullivan", "katie smith", "jenny doe", "john doe"]
new_names = list(map(lambda name: name.title(), names))
print(new_names) # ['Jack Sullivan', 'Katie Smith', 'Jenny Doe', 'John Doe']
filter():
Selecting Items from a List
The filter()
function is used to remove elements that don’t meet a specific condition.
numbers = [10, 25, 30, 45, 50, 60]
# Select only numbers greater than 30
filtered = list(filter(lambda x: x > 30, numbers))
print(filtered) # [45, 50, 60]
Another example: filtering even numbers from a list.
nums = [1, 2, 3, 4, 5, 6, 7, 8]
even_nums = list(filter(lambda x: x % 2 == 0, nums))
print(even_nums) # [2, 4, 6, 8]
reduce():
Aggregating Values
Unlike map()
and filter()
, which return iterators, reduce()
reduces a list to a single value by applying a function cumulatively. To use reduce()
, you need to import it from functools
.
from functools import reduce
numbers = [1, 2, 3, 4, 5]
# Calculate the product of all numbers
product = reduce(lambda x, y: x * y, numbers)
print(product) # 120 (1 * 2 * 3 * 4 * 5)
Another common use case: Finding the maximum value in a list.
max_value = reduce(lambda x, y: x if x > y else y, numbers)
print(max_value) # 5
all():
Returns True
if all elements meet a condition.
numbers = [2, 4, 6, 8, 10]
# Check if all numbers are even
all_even = all(num % 2 == 0 for num in numbers)
print(all_even) # True
any():
Returns True
if at least one element meets a condition.
numbers = [1, 3, 5, 7, 8]
# Check if there is at least one even number
has_even = any(num % 2 == 0 for num in numbers)
print(has_even) # True
Lists in Data Science
Lists are fundamental in data science because they provide a flexible way to store and manipulate collections of data. While libraries like NumPy and Pandas offer optimized data structures (ndarray
, DataFrame
), lists remain essential for handling raw data, processing text, and storing intermediate results. Therefore, if you intend to learn machine learning and improve yourself in data science or AI, you should start by mastering lists and data types in Python.
1. Storing and Processing Data
Lists can store numerical data, text data, or even a mix of both, making them useful for organizing datasets before loading them into specialized libraries.
data = [
["Alice", 25, "Engineer"],
["Bob", 30, "Doctor"],
["Charlie", 22, "Designer"]
]
# Extract all ages
ages = [row[1] for row in data]
print(ages) # [25, 30, 22]
2. Converting Lists to NumPy Arrays
NumPy arrays are often used for numerical computations, but they start as Python lists.
import numpy as np
numbers = [1, 2, 3, 4, 5]
numpy_array = np.array(numbers)
print(numpy_array) # [1 2 3 4 5]
3. Using Lists for Feature Engineering
In machine learning, lists help preprocess text data, extract features, and store results before feeding them into models.
text_data = ["Hello world", "Python is great", "Data science is fun"]
# Convert sentences into word lists
tokenized_text = [sentence.split() for sentence in text_data]
print(tokenized_text)
# [['Hello', 'world'], ['Python', 'is', 'great'], ['Data', 'science', 'is', 'fun']]
4. Filtering and Cleaning Data
Lists are used to clean raw data by removing missing values, filtering noise, or normalizing text.
data = ["Python", "", "Machine Learning", " ", "AI", None]
# Remove empty and None values
cleaned_data = list(filter(lambda x: x and x.strip(), data))
print(cleaned_data) # ['Python', 'Machine Learning', 'AI']
5. Storing Predictions and Results
Machine learning models generate predictions that are often stored in lists before further processing.
predictions = [0.85, 0.92, 0.78, 0.95]
# Convert predictions to percentage
percentages = list(map(lambda x: f"{x * 100:.2f}%", predictions))
print(percentages) # ['85.00%', '92.00%', '78.00%', '95.00%']
Conclusion
Python lists are versatile and powerful, offering numerous built-in methods for manipulation and iteration. Understanding how to efficiently slice, concatenate, iterate, and modify lists ensures you can work with collections effectively. Whether you're building simple scripts or large-scale applications, mastering lists will improve your Python programming skills.