Built-in Functions and Operations for Python Sequences

Sequences in Python divide into mutable and immutable categories. Lists permit modification after creation, while tuples and strings do not.

Membership Operators

Use in and not in to verify whether a value appears inside a sequence.

'x' in 'python'
# False

'z' not in 'hello'
# True

The del Statement

del removes references. It can eliminate entire variables or excise portions of a mutable sequence.

Deleting names from the namespace:

label = "demo"
nums = [10, 20, 30, 40]
del label, nums

Slicing out a contiguous range:

nums = [10, 20, 30, 40, 50]
del nums[1:4]
# nums is now [10, 50]

Because del accepts a step, it can remove scattered elements. An empty-list assignment to an extended slice with a stride often fails when the lengths do not align, whereas del succeeds:

nums = [1, 2, 3, 4, 5]
del nums[::2]
# nums becomes [2, 4]

Boolean Reductions: all() and any()

all() yields True when every item is truthy. any() yields True when at least one item is truthy.

a = [5, 7, 0, 2]
b = [3, 8, 9]

all(a)  # False
all(b)  # True
any(a)  # True
any(b)  # True

Indexed Enumeration

enumerate() pairs each element with an incrementing counter, returning an iterator of (index, value) tuples. Materialize it with list().

months = ["Jan", "Feb", "Mar", "Apr"]
list(enumerate(months))
# [(0, 'Jan'), (1, 'Feb'), (2, 'Mar'), (3, 'Apr')]

Shift the starting index with the start keyword:

list(enumerate(months, start=101))
# [(101, 'Jan'), (102, 'Feb'), (103, 'Mar'), (104, 'Apr')]

Parallel Aggregation with zip()

zip() combines multiple iterables element-wise. The resulting iteratro stops at shortest input sequence.

keys = ["id", "score", "rank"]
vals = [42, 98.5]
list(zip(keys, vals))
# [('id', 42), ('score', 98.5)]

When lengths differ across three inputs, truncation still applies to the minimum length:

a = [1, 2, 3]
b = [10, 20, 30, 40]
c = "xy"
list(zip(a, b, c))
# [(1, 10, 'x'), (2, 20, 'y')]

Casting Between Sequence Types

list(), tuple(), and str() build new sequences from existing iterables.

String to list:

list("abc")       # ['a', 'b', 'c']
list((7, 8, 9))   # [7, 8, 9]

String to tuple:

tuple("xyz")      # ('x', 'y', 'z')

Sequence to string representation:

str([7, 8, 9])    # '[7, 8, 9]'

Min, Max, and Aggregation

min() and max() support a default fallback for empty inputs:

scores = [88, 92, 75]
min(scores, default=0)     # 75

empty = []
min(empty, default="N/A")  # 'N/A'

len() returns the element count. sum() totals numeric items and can begin from an arbitrary start:

prices = [10, 20, 30]
len(prices)      # 3
sum(prices)      # 60
sum(prices, 5)   # 65

Sorting and Reversing

sorted() produces a new ordered list and leaves the original unchanged. reversed() returns an iterator that traverses the sequence backward.

values = [10, 2, 3, 0, 12, 23, 2]
sorted(values)
# [0, 2, 2, 3, 10, 12, 23]

Reverse traversal:

values = [1, 2, 3, 4]
list(reversed(values))
# [4, 3, 2, 1]

Tags: python Sequences built-in functions Lists Tuples

Posted on Tue, 06 Oct 2026 16:37:27 +0000 by agsparta