Python List Processing Example

This beginner-friendly example shows how to process a Python list step by step.

You will learn how to:

  • loop through a list
  • filter items based on a condition
  • create a new list with changed values
  • summarize list data with functions like sum() and len()

The goal is to practice real list processing in one small script, without getting distracted by too much theory.

Quick example

numbers = [3, 7, 2, 8, 5]

# keep only even numbers
evens = []
for n in numbers:
    if n % 2 == 0:
        evens.append(n)

# make a new list with doubled values
doubled = []
for n in numbers:
    doubled.append(n * 2)

print("original:", numbers)
print("evens:", evens)
print("doubled:", doubled)
print("sum:", sum(numbers))

Use this as a simple list processing pattern: loop through the list, check each item, and build a new result list when needed.

What this example teaches

This example focuses on practical list work:

  • It shows how to work through a list one item at a time.
  • It demonstrates filtering values based on a condition.
  • It demonstrates creating a new list from existing values.
  • It shows simple summary operations like total and count.
  • It keeps the focus on practical list processing, not list theory.

If you are new to lists, see Python lists explained for beginners.

The example input list

We will use a small list of numbers so the result is easy to follow:

numbers = [3, 7, 2, 8, 5]

This list contains five items. Python will process them in order:

  1. 3
  2. 7
  3. 2
  4. 8
  5. 5

A useful beginner idea: the original list does not change unless you modify it directly. In this example, we read from numbers and build new lists instead.

Looping through the list

A for loop lets you read one item at a time:

numbers = [3, 7, 2, 8, 5]

for n in numbers:
    print(n)

Expected output:

3
7
2
8
5

Here:

  • numbers is the list
  • n is the loop variable
  • on each loop, n holds one value from the list

If you want more practice with this step, read how to loop through a list in Python.

For beginners, it can help to trace the loop with print():

numbers = [3, 7, 2, 8, 5]

for n in numbers:
    print("current number:", n)

Filtering items

Filtering means keeping only the items that match a rule.

In this example, the rule is: keep only even numbers.

numbers = [3, 7, 2, 8, 5]
evens = []

for n in numbers:
    if n % 2 == 0:
        evens.append(n)

print(evens)

Expected output:

[2, 8]

How it works:

  • evens = [] creates an empty list
  • the loop reads each number
  • if n % 2 == 0 checks whether the number is even
  • evens.append(n) adds matching values to the new list

This is a clear beginner pattern:

  1. start with an empty list
  2. loop through the original list
  3. check a condition
  4. append matching items

For more filtering examples, see how to filter a list in Python.

Transforming items

Transforming means creating a new list where each item is changed in some way.

Here, we will multiply each number by 2:

numbers = [3, 7, 2, 8, 5]
doubled = []

for n in numbers:
    doubled.append(n * 2)

print(doubled)

Expected output:

[6, 14, 4, 16, 10]

This does not change the original list. It creates a new list called doubled.

For beginners, building a new list is often easier to understand than changing the original list in place. It also makes debugging easier because you can compare the old list and the new list.

Summarizing list data

After processing a list, you often want a summary.

For numeric lists, common summary tools are:

  • sum() for the total
  • len() for the number of items
  • min() for the smallest value
  • max() for the largest value

Example:

numbers = [3, 7, 2, 8, 5]

print("sum:", sum(numbers))
print("count:", len(numbers))
print("smallest:", min(numbers))
print("largest:", max(numbers))

Expected output:

sum: 25
count: 5
smallest: 2
largest: 8

If you want to understand these functions better, see:

Expected output

Here is the full example again:

numbers = [3, 7, 2, 8, 5]

evens = []
for n in numbers:
    if n % 2 == 0:
        evens.append(n)

doubled = []
for n in numbers:
    doubled.append(n * 2)

print("original:", numbers)
print("evens:", evens)
print("doubled:", doubled)
print("sum:", sum(numbers))

Expected output:

original: [3, 7, 2, 8, 5]
evens: [2, 8]
doubled: [6, 14, 4, 16, 10]
sum: 25

This output shows:

  • the original list
  • the filtered list of even numbers
  • the transformed list with doubled values
  • the total of the original numbers

Beginner mistakes to watch for

These are common problems when processing lists:

  • Forgetting the colon : after for or if
  • Using append without parentheses
  • Changing the original list by accident
  • Trying to use sum() on a list that contains non-numbers

Example of a missing colon:

for n in numbers
    print(n)

Correct version:

for n in numbers:
    print(n)

Example of incorrect append use:

evens.append

Correct version:

evens.append(n)

Other common causes of errors include:

  • confusing filtering with modifying the original list
  • using the wrong indentation inside the loop
  • appending the wrong value to the result list
  • trying to use string methods on list items that are numbers
  • mixing strings and integers in summary operations

If you get an index problem while testing lists, see IndexError: list index out of range. If you get a type-related loop problem, see TypeError: int object is not iterable.

Useful debug prints:

print(numbers)
print(n)
print(evens)
print(doubled)
print(type(numbers[0]))

These can help you check:

  • what is inside the list
  • what the current loop value is
  • what has been added to your new lists
  • what data type you are working with

FAQ

What does list processing mean in Python?

It means reading items from a list and doing something with them, such as filtering, changing, or summarizing them.

Should I change the original list or create a new one?

For beginners, creating a new list is often easier to understand and safer.

Can I do this with strings instead of numbers?

Yes. You can loop through a list of strings and filter or change each item in the same way.

Is this the same as a list comprehension?

Not exactly. A list comprehension is a shorter way to build a list, but the loop-based version is usually clearer when you are learning. After you understand this pattern, you can move on to how to use list comprehensions in Python.

See also

Try the same pattern with a list of names or prices next. Then move on to a filtering tutorial or a reference page for sum() and len() to build your confidence.