Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
📦 Unit 2 – Strings, Lists, Tuples & Dictionaries  ·  Lesson 21

Lists: Operations & Methods

What is a list?

A list is an ordered, mutable collection of items written in square brackets [ ] and separated by commas. Items can be of any type, even other lists. Think of a list as a row of numbered lockers: each locker has an index and you can change what is inside.

marks →780-4921-3652-2883-1
A list of four marks with positive and negative indexes

Creating lists

Python
marks = [78, 92, 65, 88]
mixed = ['Riya', 16, 5.4, True]
empty = []
from_range = list(range(1, 6))
from_text = list('hey')
print(marks, mixed, empty)
print(from_range, from_text)
[78, 92, 65, 88] ['Riya', 16, 5.4, True] [] [1, 2, 3, 4, 5] ['h', 'e', 'y']

Indexing, slicing and changing items

Indexing and slicing work exactly like strings. The big difference: lists are mutable, so you can change an item by assigning to its index.

Python
L = [10, 20, 30, 40, 50]
print(L[0], L[-1], L[1:4], L[::-1], len(L))
L[2] = 35                       # lists are mutable
print(L)
10 50 [20, 30, 40] [50, 40, 30, 20, 10] 5 [10, 20, 35, 40, 50]
L →100201352403504
L[2] = 35 replaced 30 in the same list

List operations

Python
L = [10, 20, 35, 40, 50]
print([1, 2] + [3, 4])          # concatenation
print([0] * 5)                  # repetition
print(30 in L, 35 in L)         # membership
[1, 2, 3, 4] [0, 0, 0, 0, 0] False True

Traversing a list

Python
marks = [56, 78, 91, 45, 83]
for m in marks:                       # by value
    print(m, end=' ')
print()
for i in range(len(marks)):           # by index – needed to change items
    marks[i] = marks[i] + 2           # grace marks
print(marks)
56 78 91 45 83 [58, 80, 93, 47, 85]
💡 Remember

Changing the loop variable m in for m in marks does not change the list. To modify items, loop over indexes with range(len(marks)).

List methods

Python
m = [45, 12, 78, 34]
m.append(90);       print(m)     # add at end
m.insert(1, 60);    print(m)     # add at position 1
m.extend([5, 8]);   print(m)     # add many items
print(m.pop(), m)                # remove last, return it
print(m.pop(0), m)               # remove at index 0
m.remove(78);       print(m)     # remove first 78
print(m.index(34), m.count(12))
[45, 12, 78, 34, 90] [45, 60, 12, 78, 34, 90] [45, 60, 12, 78, 34, 90, 5, 8] 8 [45, 60, 12, 78, 34, 90, 5] 45 [60, 12, 78, 34, 90, 5] [60, 12, 34, 90, 5] 2 1
[45, 12, 78, 34]startappend(90)[45, 12, 78, 34, 90]insert(1, 60)[45, 60, 12, 78, 34, 90]extend([5, 8])[45, 60, 12, 78, 34, 90, 5, 8]pop() → 8[45, 60, 12, 78, 34, 90, 5]pop(0) → 45[60, 12, 78, 34, 90, 5]remove(78)[60, 12, 34, 90, 5]
How the list changes step by step
Python
L = [1, 2]
L.remove(5)
ValueError: list.remove(x): x not in list
MethodActionReturns
append(x)Add x at the endNone
insert(i, x)Add x at index iNone
extend(seq)Add every item of seqNone
pop(i)Remove item at i (default last)the removed item
remove(x)Remove first xNone (ValueError if absent)
index(x)Position of first xint
count(x)How many xint
sort(), sort(reverse=True)Sort in placeNone
reverse()Reverse in placeNone
clear()Remove all itemsNone

Built-in functions and sort vs sorted

Python
m = [60, 12, 34, 90, 5]
m.sort();               print(m)
m.sort(reverse=True);   print(m)
m.reverse();            print(m)
print(len(m), max(m), min(m), sum(m))
n = [3, 1, 2]
print(sorted(n), n)       # sorted() gives a NEW list
print(n.sort())           # sort() changes n and returns None
del n[0]; print(n)
[5, 12, 34, 60, 90] [90, 60, 34, 12, 5] [5, 12, 34, 60, 90] 5 90 5 201 [1, 2, 3] [3, 1, 2] None [2, 3]

Taking a list as input

Python
n = int(input('How many numbers? '))
nums = []
for i in range(n):
    nums.append(int(input('Enter number: ')))
print('List:', nums)
print('Sum =', sum(nums), '| Average =', sum(nums) / n)
print('Largest =', max(nums), '| Smallest =', min(nums))
How many numbers? 4 Enter number: 15 Enter number: 42 Enter number: 8 Enter number: 23 List: [15, 42, 8, 23] Sum = 88 | Average = 22.0 Largest = 42 | Smallest = 8

Short cut when the user types numbers separated by spaces: nums = [int(x) for x in input().split()].

List comprehension

A list comprehension builds a new list in one line: [expression for item in sequence if condition].

Python
nums = [12, 7, 30, 15, 8, 21, 4]
even = [x for x in nums if x % 2 == 0]
odd = [x for x in nums if x % 2 != 0]
squares = [x * x for x in range(1, 6)]
print('Even:', even, '| Odd:', odd)
print('Squares:', squares)
Even: [12, 30, 8, 4] | Odd: [7, 15, 21] Squares: [1, 4, 9, 16, 25]

Summary & practice

  • List = ordered, mutable, in [ ]; items of any type.
  • Indexing/slicing like strings; L[i] = x changes an item.
  • Add: append, insert, extend. Remove: pop, remove, del, clear.
  • sort() in place vs sorted() new list.
✅ Practice

Predict: L = [5, 3, 8]; L.append([1, 2]); print(len(L)) → ? L = [5, 3, 8]; L.extend('ab'); print(L) → ? Write a program to count how many numbers in a list are greater than the average.

Answers: 4; [5, 3, 8, 'a', 'b']

Frequently Asked Questions

What is the difference between append() and extend()?
append(x) adds x as one single item at the end. extend(list) adds each item of the given list separately. [1,2].append([3,4]) gives [1, 2, [3, 4]], while extend gives [1, 2, 3, 4].
What is the difference between pop(), remove() and del?
pop(i) removes the item at index i (last by default) and returns it. remove(x) removes the first occurrence of the value x. del L[i] or del L[a:b] deletes by index or slice and returns nothing.
What is the difference between sort() and sorted()?
L.sort() sorts the list in place and returns None. sorted(L) returns a new sorted list and leaves L unchanged; it also works on tuples and strings.
← Class 11 Computer Science
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

Next topicNested Lists, Copying & List Programs Your progress: 21 / 29 (72%) · Lesson 22 / 29 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free