Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
🐍 Unit 2 – Python Programming  ·  Lesson 08

Lists in Python

What is a list?

A list stores many values under one name, in order, inside square brackets. It is mutable — items can be added, removed or changed. Lists are the first step towards working with datasets: a column of marks, a week of temperatures, a shopping cart.

cart →Milk0-4Bread1-3Eggs2-2Butter3-1
Each item has a positive and a negative index

Accessing and changing items

Python
cart = ['Milk', 'Bread', 'Eggs', 'Butter']
prices = [56, 40, 84, 60]
print(cart[0], cart[-1], prices[1:3])
cart[1] = 'Brown Bread'            # change an item
print(cart)
print(len(cart), 'Eggs' in cart)
Milk Butter [40, 84] ['Milk', 'Brown Bread', 'Eggs', 'Butter'] 4 True

List operations

Python
week1 = [12, 15, 9]
week2 = [20, 18]
print(week1 + week2)          # concatenation
print(['-'] * 5)              # repetition
print(week1 * 2)
all_sales = week1 + week2
print(all_sales[::-1], all_sales[::2])
[12, 15, 9, 20, 18] ['-', '-', '-', '-', '-'] [12, 15, 9, 12, 15, 9] [18, 20, 9, 15, 12] [12, 9, 18]
OperationOperatorExample
Concatenation+[1, 2] + [3] → [1, 2, 3]
Repetition*[0] * 3 → [0, 0, 0]
Membershipin, not in'Eggs' in cart → True
Slicing[start:stop:step]L[1:3], L[::-1]

Traversing a list

Python
cart = ['Milk', 'Bread', 'Eggs', 'Butter']
prices = [56, 40, 84, 60]
total = 0
for i in range(len(cart)):
    print(f'{i + 1}. {cart[i]:<7} Rs.{prices[i]}')
    total += prices[i]
print('Total: Rs.', total)
1. Milk Rs.56 2. Bread Rs.40 3. Eggs Rs.84 4. Butter Rs.60 Total: Rs. 240

List methods

Python
scores = [72, 88, 65]
scores.append(91)          ; print(scores)
scores.insert(0, 59)       ; print(scores)
scores.extend([77, 80])    ; print(scores)
scores.remove(65)          ; print(scores)
last = scores.pop()        ; print(last, scores)
print(scores.index(91), scores.count(88))
scores.sort()              ; print(scores)
scores.reverse()           ; print(scores)
[72, 88, 65, 91] [59, 72, 88, 65, 91] [59, 72, 88, 65, 91, 77, 80] [59, 72, 88, 91, 77, 80] 80 [59, 72, 88, 91, 77] 3 1 [59, 72, 77, 88, 91] [91, 88, 77, 72, 59]
➕
Addappend(x), insert(i, x), extend(list)
➖
Removeremove(x), pop(), pop(i), clear(), del L[i]
🔍
Findindex(x), count(x)
↕️
Arrangesort(), sort(reverse=True), reverse()
List methods grouped by job

Built-in functions on lists

Python
rain = [12.5, 0, 33.2, 8.4, 0, 51.0, 19.3]
print('Total:', sum(rain), '| Max:', max(rain), '| Min:', min(rain))
print('Average:', round(sum(rain) / len(rain), 2))
print('Sorted:', sorted(rain))
print('Dry days:', rain.count(0))
Total: 124.4 | Max: 51.0 | Min: 0 Average: 17.77 Sorted: [0, 0, 8.4, 12.5, 19.3, 33.2, 51.0] Dry days: 2
Rainfall list as a chart0204060mmD1D2D3D4D5D6D712.5033.28.405119.3
max = 51.0 on day 6; two dry days

Data-style list programs

1. Separate pass and fail

Python
marks = [45, 78, 32, 91, 66, 29, 84]
passed, failed = [], []
for m in marks:
    if m >= 33:
        passed.append(m)
    else:
        failed.append(m)
print('Passed:', passed)
print('Failed:', failed)
print('Pass %:', round(len(passed) / len(marks) * 100, 1))
Passed: [45, 78, 91, 66, 84] Failed: [32, 29] Pass %: 71.4

2. Input a list and search

Python
n = int(input('How many items? '))
items = []
for i in range(n):
    items.append(input('Item: '))
key = input('Search: ')
if key in items:
    print(key, 'found at position', items.index(key) + 1)
else:
    print(key, 'not found')
How many items? 3 Item: Pen Item: Ruler Item: Eraser Search: Ruler Ruler found at position 2

3. Convert and filter with list comprehension

Python
temps_c = [30, 25, 37, 41]
temps_f = [c * 9 / 5 + 32 for c in temps_c]
print(temps_f)
high = [c for c in temps_c if c > 35]
print(high)
[86.0, 77.0, 98.6, 105.8] [37, 41]

Summary & practice

  • List = ordered, mutable collection in [ ].
  • Index from 0 (or −1 from the end); slice with [a:b:c].
  • + joins, * repeats, in checks membership.
  • Methods: append, insert, extend, remove, pop, index, count, sort, reverse.
  • Functions: len, max, min, sum, sorted.
✅ Practice

1) Store the runs scored by a batsman in 6 matches and find the total, average, best and number of half-centuries (≥ 50). 2) From a list of prices, make a new list with 10% discount applied. 3) Predict: L = [3, 1, 2]; L.append(L.pop(0)); print(L).

Answer 3: [1, 2, 3]

Frequently Asked Questions

Can a list contain values of different types?
Yes. A list like ["Riya", 16, 92.5, True] is valid, though for data analysis we usually keep one type per list.
What does pop() return?
pop() removes and returns the last item; pop(i) removes and returns the item at index i.
What is the difference between sort() and sorted()?
list.sort() changes the original list and returns None; sorted(list) returns a new sorted list and keeps the original unchanged.
← Class 11 Informatics Practices
🔗

Share this topic with a friend

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

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

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

Next topicDictionaries in Python Your progress: 8 / 20 (40%) · Lesson 9 / 20 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