Lists in Python for Data Science

Datatypes Mar 19, 2026

Lists in Python for Data Science – Complete Guide 2026

Lists are one of the most fundamental and frequently used data structures in Python data science. They are flexible, dynamic, and perfect for storing sequences of data such as feature names, model predictions, row records, or intermediate results.

TL;DR — Key Features of Lists

  • Ordered, mutable, and allows duplicates
  • Can hold mixed data types
  • Dynamic size (can grow and shrink)
  • Very fast for append operations

1. Creating and Basic Operations

# Creating lists
features = ["amount", "quantity", "profit", "region", "category"]
scores = [85, 92, 78, 95, 88]

# Common operations
features.append("log_amount")                    # Add item
features.insert(0, "customer_id")                # Insert at position
features.remove("category")                      # Remove by value
popped = features.pop()                          # Remove and return last item

print(features)
print(len(features))                             # Length

2. Real-World Data Science Usage

import pandas as pd

# Small inline sample so this example runs as-is.
# In a real project you would load it: df = pd.read_csv("sales_data.csv")
df = pd.DataFrame({
    "customer_id": [101, 102, 103, 104, 105, 106],
    "customer_name": ["Ana", "Ben", "Chen", "Dana", "Eli", "Fay"],
    "order_date": ["2026-01-05", "2026-01-12", "2026-02-03",
                   "2026-02-18", "2026-03-01", "2026-03-09"],
    "amount": [1250.75, 890.50, 2340.00, 1780.25, 2615.40, 450.00],
    "quantity": [3, 2, 5, 4, 6, 1],
    "profit": [312.69, 222.63, 585.00, 445.06, 653.85, 112.50],
    "region": ["North", "South", "East", "North", "West", "South"],
    "category": ["Electronics", "Books", "Electronics", "Home", "Home", "Books"],
    "rating": [5, 4, None, 3, 5, 4],
})

# Example 1: Dynamic list of column names
numeric_cols = [col for col in df.columns if df[col].dtype in ["int64", "float64"]]

# Example 2: Building a list of processed records
processed_records = []
for row in df.itertuples():
    record = {
        "customer_id": row.customer_id,
        "amount": row.amount,
        "profit": round(row.amount * 0.25, 2)
    }
    processed_records.append(record)

result_df = pd.DataFrame(processed_records)

# Example 3: List of feature names for model training
feature_list = ["amount", "quantity", "profit", "region", "log_amount", "is_weekend"]

3. List Comprehensions vs Traditional Loops

# List comprehension (preferred for simple cases)
squared = [x ** 2 for x in scores if x > 80]

# Traditional loop (better for complex logic)
high_value = []
for x in scores:
    if x > 80:
        high_value.append(x * 1.1)

4. Best Practices for Lists in Data Science 2026

  • Use list comprehensions for simple transformations and filtering
  • Use generator expressions when working with large data (memory efficient)
  • Avoid using lists for very large numeric data — prefer NumPy arrays instead
  • Use .append() in loops when building lists dynamically
  • Consider collections.deque for frequent appends/pops from both ends

How Lists Work Under the Hood

A Python list is a dynamic array of references. Reading or replacing an item by index (items[3]) is fast no matter how long the list is, and append() is fast on average because Python over-allocates space. Inserting or removing at the front, however, shifts every other element, so insert(0, x) and pop(0) get slower as the list grows.

Because a list stores references, it can hold any mix of objects — numbers, strings, dicts, even other lists. Slicing (items[1:4]) always returns a new list, while methods like sort() and append() change the list in place and return None.

Worked Example: Filtering and Summarising Model Scores

runs = [
    {"model": "rf", "score": 0.81},
    {"model": "xgb", "score": 0.86},
    {"model": "logreg", "score": 0.74},
    {"model": "lgbm", "score": 0.85},
]

# Keep good runs, best first
good = [r for r in runs if r["score"] >= 0.8]
good.sort(key=lambda r: r["score"], reverse=True)

names = [r["model"] for r in good]
print(names)                      # ['xgb', 'lgbm', 'rf']
print(names[:2])                  # top two via slicing
avg = sum(r["score"] for r in good) / len(good)
print(f"Average of good runs: {avg:.3f}")

This example combines the main list skills: a comprehension for filtering, sort(key=...) for ordering, slicing for the top results, and a generator expression inside sum() for an aggregate.

Common Mistakes

# 1. Assigning the result of an in-place method
nums = [3, 1, 2]
result = nums.sort()
print(result)            # None - sort() changes nums and returns None

# 2. Creating nested lists with *
grid = [[0] * 3] * 2
grid[0][0] = 9
print(grid)              # [[9, 0, 0], [9, 0, 0]] - both rows are the same object
grid = [[0] * 3 for _ in range(2)]   # correct

# 3. Removing items while looping over the same list
vals = [1, 2, 2, 3]
vals = [v for v in vals if v != 2]   # build a new list instead
print(vals)
  • Do not name a variable list; it hides the built-in function
  • Use copy() or slicing (items[:]) when you need an independent copy

Quick Summary

  • Lists are ordered, mutable and fast for indexing and appending
  • In-place methods return None; slicing and comprehensions return new lists
  • Watch out for shared references in nested lists and for removing while iterating

Conclusion

Lists remain one of the most versatile data structures in Python for data science. In 2026, use them confidently for storing feature names, building result sets, creating dynamic column lists, and intermediate processing steps. Combine lists with list comprehensions and generators to write clean, efficient, and Pythonic data science code.

Next steps:

  • Review how you currently use lists in your projects and optimize them with comprehensions or generators where appropriate

Related Tutorials

Share:
Last updated: March 2026
Built-in Functions in Python 2026 str() in Python 2026: String Conversion + Modern Formatting & Best Practices memoryview with JAX in Python 2026: Zero-Copy NumPy → JAX Array Interop + Efficient ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + GPU Pinning & ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + ML Examples memoryview with NumPy & PyTorch in Python 2026: Zero-Copy Views, Efficient Slicing & ML Interop Examples memoryview with NumPy in Python 2026: Zero-Copy Views, Efficient Slicing & Real ML Examples memoryview() in Python 2026: Zero-Copy Magic for Large Binary Data + Real Examples import() in Python 2026: How It Works, Security Risks & Modern Alternatives (importlib) zip() in Python 2026: How to Use It, strict=True Behavior & Real-World Examples vars() in Python 2026: Accessing Object Namespace + Modern Introspection Patterns type() in Python 2026: Dynamic Type Inspection & Object Creation + Modern Patterns tuple() in Python 2026: Immutable Sequences + Modern Patterns & Best Practices super() in Python 2026: Method Resolution & Modern Inheritance Patterns sum() in Python 2026: Summing Iterables + Modern Numeric Patterns & Best Practices staticmethod() in Python 2026: Static Methods, Utility Functions & Modern Best Practices sorted() in Python 2026: Sorting Iterables + Modern Patterns & Best Practices slice() in Python 2026: Creating Slice Objects + Modern Patterns & Best Practices setattr() in Python 2026: Dynamic Attribute Setting + Modern Patterns & Safety set() in Python 2026: Mutable Sets Creation + Modern Patterns & Best Practices