Skip to content
Educora
IntermediateGrade 925 min35 / 59

Lists and list operations

Creating lists, reading n numbers into an empty list, indexes and neighbouring elements, list methods and DİM-style tracing tasks.

Check yourself
In this lesson you will learn
  • Create a list, collect n numbers entered from the keyboard in an empty list and reach any element by its index (negative ones included).
  • Walk through a list by value and by index and use the neighbours a[i - 1] and a[i + 1] without leaving the list.
  • Apply the methods append, insert, remove, pop, reverse, sort, count, index and the functions len, sum, min, max.
  • Trace step by step a program that changes elements inside a loop, and build a new list by a condition.

“There are 25 students — do we need 25 separate variables for their scores?” No: a list keeps all the values under one name. Lists appeared in every DİM entrance exam in informatics in 2025–2026: a program changes elements inside a loop, ends with print(a), and the five answer options often differ by a single element. The written part also likes the sentence “n integers are entered from the keyboard into an empty list…”. In this lesson you will learn lists well enough to follow every element step by step.

List, element and index

Definition
List

A sequence of elements separated by commas in square brackets: a = [7, 3, 9, 3, 5]. The order is kept, a value may occur several times, and the list can be changed after it is created: elements can be replaced, added and deleted. An empty list is written [].

Definition
Index

The position number of an element in the list; it starts at 0. In a list of n elements the indexes are 0, 1, …, n − 1, and a[i] is the element with index i. Negative indexes count from the end: a[-1] is the last element, a[-2] the one before it.

index0123473935a-5-4-3-2-1negative index — counted from the end
len(a) = 5, the last index is len(a) - 1 = 4; a[4] and a[-1] are the same element.

Remember three rules. len(a) gives the number of elements, so the last index is len(a) - 1; writing a[len(a)] raises an IndexError. The assignment a[i] = x replaces an element in place — the length of the list does not change. The expression x in a checks whether the value x is in the list and gives True or False.

Python
a = [7, 3, 9, 3, 5]
print(len(a), a[0], a[-1])
a[1] = a[1] * 10
a[-1] = a[0] + a[2]
print(a)
print(3 in a, 5 in a)
▸ Expected output
5 7 5
[7, 30, 9, 3, 16]
True False

In the exam a list is usually filled from the keyboard. Two forms are common. First: n, then n numbers each on its own line — you create an empty list and add each number to its end with append. Second: all values on one line separated by spaces — then input().split(' ') cuts the line into pieces. Careful: split returns a list of strings; convert them to numbers with int before calculating.

Python
# form 1: n, then n numbers, each on its own line
n = int(input())
a = []
for i in range(0, n):
    a.append(int(input()))

# form 2: all numbers on one line, e.g. 12 5 8
b = input().split(' ')
for i in range(0, len(b)):
    b[i] = int(b[i])
The exam version with input(). input() does not work in the browser, so in the next example the lines that would come from the keyboard are stored in variables.
Python
lines = ['3', '4', '-2', '7']    # what the keyboard would give
n = int(lines[0])
a = []
for i in range(1, n + 1):
    a.append(int(lines[i]))
print(a)

b = '12 5 8'.split(' ')
print(b, b[0] + b[1])
for i in range(0, len(b)):
    b[i] = int(b[i])
print(b, b[0] + b[1])
▸ Expected output
[4, -2, 7]
['12', '5', '8'] 125
[12, 5, 8] 17

Walking through a list: by value and by index

In for x in a: the variable x takes the values of the elements one by one — handy for reading, but x = … does not change the list, because x is only a copy. In for i in range(0, len(a)): the variable i takes the indexes: a[i] is the element itself, so you can change it, add its index to a sum and look at its neighbours. Most DİM list tasks are written in the second form.

Python
a = [4, 7, 1, 8]
for x in a:
    x = x * 2
print(a)
for i in range(0, len(a)):
    a[i] = a[i] * 2
print(a)
s = 0
for i in range(0, len(a)):
    if a[i] > 5:
        s = s + i
print(s)
▸ Expected output
[4, 7, 1, 8]
[8, 14, 2, 16]
4

When you work with neighbouring elements, watch the loop limits: a[i + 1] exists only when i ≤ len(a) − 2, and a[i - 1] makes sense only when i ≥ 1. At i = 0, a[i - 1] gives no error — it is a[-1], the last element. The table shows which loop header you need for which task.

What you needLoop headerValues of i
every elementfor i in range(0, len(a))0 … n − 1
a[i] and a[i + 1]for i in range(0, len(a) - 1)0 … n − 2
a[i - 1] and a[i]for i in range(1, len(a))1 … n − 1
a[i - 1], a[i] and a[i + 1]for i in range(1, len(a) - 1)1 … n − 2
n = len(a). The right end of range is not included.
Python
a = [2, 5, 3, 1, 4, 9, 1, 7]
s = 0
for i in range(1, len(a) - 1):
    if a[i] > a[i - 1] and a[i] > a[i + 1]:
        s = s + a[i]
        a[i] = 0
print(s, a)
▸ Expected output
17 [2, 0, 0, 1, 4, 0, 1, 7]
DİM-style task (closed)

Determine the result of the program above.
A) 14 [2, 0, 3, 1, 4, 0, 1, 7]
B) 17 [2, 0, 0, 1, 4, 0, 1, 7]
C) 17 [2, 5, 3, 1, 4, 9, 1, 7]
D) 21 [2, 0, 0, 1, 4, 0, 0, 7]
E) 14 [2, 5, 3, 1, 4, 9, 1, 7]

Show solution
i runs from 1 to 6 (range(1, 7)). At every step we look at the current state of the list:
i = 1: 5 > 2 and 5 > 3 → s = 5, a[1] = 0
i = 2: 3 > 0 (a[1] is already 0) and 3 > 1 → s = 8, a[2] = 0
i = 3: 1 > 0, but 1 > 4 is false
i = 4: 4 > 1, but 4 > 9 is false
i = 5: 9 > 4 and 9 > 1 → s = 17, a[5] = 0
i = 6: 1 > 0, but 1 > 7 is false
Answer: B. Option A is the mistake of those who look at the original list: they miss the 3.

List methods and built-in functions

A method is written after the list name with a dot: a.append(5). A function takes the list in parentheses: len(a). The methods append, insert, remove, pop, reverse, sort change the list itself; count and index only give information. Every row of the table starts again from the list a = [5, 2, 8, 2].

CodeWhat it doesResult
a.append(4)adds 4 to the end[5, 2, 8, 2, 4]
a.insert(1, 9)puts 9 at index 1, the rest shift right[5, 9, 2, 8, 2]
a.remove(2)deletes the first occurrence of the value 2[5, 8, 2]
x = a.pop(0)deletes the element with index 0 and returns it; a.pop() takes the last onex = 5, [2, 8, 2]
a.reverse()reverses the order[2, 8, 2, 5]
a.sort()sorts in ascending order; a.sort(reverse=True) — descending[2, 2, 5, 8]
a.count(2)how many times 2 occurs2
a.index(8)the index of the first 82
len(a), sum(a)number of elements, sum4, 17
min(a), max(a)smallest, largest element2, 8
b = sorted(a)a new sorted list; a stays the sameb = [2, 2, 5, 8]
Python
a = [5, 2, 8, 2]
a.append(4)
a.insert(1, 9)
a.remove(2)
x = a.pop()
print(a, x)
a.sort()
print(a, a.count(2), a.index(8))
print(len(a), sum(a), min(a), max(a))
▸ Expected output
[5, 9, 8, 2] 4
[2, 5, 8, 9] 1 2
4 24 2 9
DİM-style task (coded answer)

Determine the number printed by the program.
a = [3, 8, 3, 5, 8, 1]
a.remove(8)
a.insert(2, a.pop())
a.append(a.count(3))
print(a.index(1) + a[-1] + len(a))

Show solution
remove(8) deletes the first 8: [3, 3, 5, 8, 1].
a.insert(2, a.pop()): the argument is evaluated first — pop() takes the last 1, a = [3, 3, 5, 8]; then 1 is put at index 2: [3, 3, 1, 5, 8].
count(3) = 2 is appended: [3, 3, 1, 5, 8, 2].
a.index(1) = 2, a[-1] = 2, len(a) = 6.
Answer: 2 + 2 + 6 = 10.

A new list by a condition. Lists and strings

  1. 1
    Empty list

    Create b = [] for the result before the loop.

  2. 2
    Loop

    Walk through the original list by value (for x in a) or by index (for i in range(0, len(a))).

  3. 3
    Condition

    Check the condition for each element, for example if x % 2 == 0:.

  4. 4
    Add

    If the condition holds, add the element (or a value made from it) with b.append(...).

  5. 5
    Print

    After the loop write print(b) — Python prints the list as [6, 15, 9], with square brackets and commas.

Python
a = [12, 7, 30, 5, 18, 7]
b = []
for x in a:
    if x % 2 == 0:
        b.append(x // 2)
print(b)
c = []
for x in a:
    if c.count(x) == 0:
        c.append(x)
print(c, len(c))
▸ Expected output
[6, 15, 9]
[12, 7, 30, 5, 18] 5

Switching between lists and strings is often needed in DİM programs. list('2026') turns a string into a list of characters: ['2', '0', '2', '6']. ''.join(c) glues a list of strings into one string (the separator is the string before the dot). s.split(' ') cuts a string at the spaces. To reverse a number use the chain str(n) → list → reverse() → join → int.

Python
n = 1720
c = list(str(n))
print(c)
c.reverse()
r = int(''.join(c))
print(r, n + r)
w = '12 5 40'.split(' ')
print(w, len(w))
print('-'.join(w))
▸ Expected output
['1', '7', '2', '0']
271 1991
['12', '5', '40'] 3
12-5-40
Python
a = [6, 1, 4, 9, 4, 2, 7, 3]
b = [4, 7, 1]
for i in range(0, len(a) - 1):
    if b.count(a[i]) > 0:
        a[i + 1] = a[i + 1] + a[i]
print(a)
▸ Expected output
[6, 1, 5, 9, 4, 6, 7, 10]
DİM-style task (closed)

Determine the result of the program above.
A) [6, 1, 5, 14, 4, 6, 7, 10]
B) [6, 5, 13, 9, 6, 2, 10, 3]
C) [6, 1, 5, 9, 4, 6, 7, 10]
D) [6, 1, 5, 9, 4, 6, 7, 3]
E) [6, 1, 4, 9, 4, 2, 7, 3]

Show solution
The condition b.count(a[i]) > 0 means “is a[i] in b?”; i runs from 0 to 6.
i = 0: 6 is not in b.
i = 1: 1 is → a[2] = 4 + 1 = 5
i = 2: a[2] is already 5, not in b (looking at the old 4 leads to A)
i = 3: 9 is not. i = 4: 4 is → a[5] = 2 + 4 = 6
i = 5: 6 is not. i = 6: 7 is → a[7] = 3 + 7 = 10
Answer: C. D forgets the last step (i = 6), B changes a[i] instead of a[i + 1].
Python
a = [3, 5, 7, 8, 10, 12, 15, 17, 19]
s = 0
i = 1
while i < len(a) - 1:
    if a[i] - a[i - 1] == a[i + 1] - a[i]:
        s = s + i
    i = i + 1
print(s)
▸ Expected output
12
DİM-style task (coded answer)

Determine the number printed by the program above.

Show solution
The condition means that a[i] lies exactly between its two neighbours: the differences on the left and on the right are equal. i runs from 1 to 7:
i = 1: 5 − 3 = 2, 7 − 5 = 2 → s = 1
i = 2: 2 and 1 — no; i = 3: 1 and 2 — no
i = 4: 10 − 8 = 2, 12 − 10 = 2 → s = 1 + 4 = 5
i = 5: 2 and 3 — no; i = 6: 3 and 2 — no
i = 7: 17 − 15 = 2, 19 − 17 = 2 → s = 5 + 7 = 12
The indexes are added, not the values. Answer: 12.
Name the method
  1. 1.a = [4, 1]; after a.(7) the list is [4, 1, 7]
  2. 2.a = [6, 2, 9]; after x = a.(0) we have x = 6, a = [2, 9]
  3. 3.a = [2, 7, 7]; after a.(7) the list is [2, 7]
  4. 4.a = [3, 5, 3, 3]; the value of a.(3) is 3
  5. 5.a = [8, 1, 5]; after a.() the list is [1, 5, 8]
  6. 6.a = [8, 1, 5]; the function (a) returns 3
Exercise

In the exam the list would be entered from the keyboard; here it is given. Collect the elements that are greater than both of their neighbours (the first and the last element are not checked) in a new list b, print b, then print the sum of the indexes of those elements.

Exercise · Python
a = [3, 9, 4, 4, 7, 2, 8, 1]
b = []
s = 0
# your loop here
▸ Expected output
[9, 7, 8]
11
Exercise

Remove the repetitions from the list a: every value stays only where it first occurs; print the new list u. Then print how many different values occur in a more than once.

Exercise · Python
a = [5, 3, 5, 2, 3, 5, 9]
u = []
k = 0
▸ Expected output
[5, 3, 2, 9]
2

Indexes, len and in work the same way for strings — you saw this in the lesson “Strings and string operations”. In the next lesson, “Functions: def, parameters and return”, we will read programs that pass every element of a list to a function (a[i] = k(a[i])). Extra tools such as slices (a[1:4]) and list comprehensions are in the Python course lesson “Lists”.

Key points

  • Indexes run from 0 to len(a) − 1; a[-1] is the last element.
  • A list from the keyboard: a = [] and a.append(int(input())) in a loop; from one line — input().split(' ') and conversion with int.
  • Walk by index to change elements and look at neighbours; narrow the loop limits for a[i ± 1].
  • remove deletes by value, pop by index; sort, reverse, append return None.
  • When tracing, always look at the current list: a changed element affects the later steps.

Check yourself

12 questions. Every correct answer earns XP.

1 / 12
In the list a = [4, 8, 15, 16, 23], what is a[-2]?