🟢 Beginner  ·  Lesson 12

Lists in Python

Python Lists

What is a List?

A list stores many values in one variable, in order, inside square brackets [ ]. Lists are mutable — you can add, remove and change items.

Python – list.py
marks = [85, 92, 78, 90]
print(marks)
print(len(marks))   # how many items
[85, 92, 78, 90] 4

Access & Slice

Python – access.py
fruits = ["apple", "banana", "mango"]
print(fruits[0])     # first
print(fruits[-1])    # last
print(fruits[0:2])   # first two
apple mango ['apple', 'banana']

List Methods

MethodDoes
.append(x)add x at the end
.insert(i,x)add x at position i
.remove(x)delete first x
.pop()remove & return last
.sort()sort in place
.reverse()reverse order
.count(x)how many x

Program 1: Create & Loop Through

Python – loop_list.py
names = ["Aman", "Riya", "Karan"]
for name in names:
    print("Student:", name)
Student: Aman Student: Riya Student: Karan

A for loop visits each item one by one — no index needed.

Program 2: Sum, Max and Min

Python – stats.py
marks = [85, 92, 78, 90, 88]
print("Total  :", sum(marks))
print("Highest:", max(marks))
print("Lowest :", min(marks))
print("Average:", sum(marks) / len(marks))
Total : 433 Highest: 92 Lowest : 78 Average: 86.6

Built-in functions sum(), max(), min(), len() work directly on lists.

Program 3: Search in a List

Python – search.py
numbers = [10, 25, 30, 45]
x = int(input("Search for: "))
if x in numbers:
    print(x, "found at position", numbers.index(x))
else:
    print(x, "not in list")
Search for: 30 30 found at position 2
  • x in numbers checks membership.
  • .index(x) gives the position of the item.

Program 4: Sort & Remove Duplicates

Python – cleanup.py
data = [5, 2, 8, 2, 5, 1]
data = list(set(data))   # set removes duplicates
data.sort()              # then sort
print(data)
[1, 2, 5, 8]

set(data) drops duplicates; list() turns it back; .sort() orders it.

List Comprehension (a peek)

A short way to build lists in one line:

Python – comp.py
squares = [x*x for x in range(1, 6)]
print(squares)
[1, 4, 9, 16, 25]

Common Mistakes

  • Index out of range — marks[10] on a 4-item list errors.
  • .remove(x) errors if x is not present.
  • Confusing append (one item) with extend (many items).

Practice Tasks

  1. Store 5 marks and print the total and average.
  2. Find the largest number in a list without using max().
  3. Count even and odd numbers in a list.
  4. Remove duplicates from a list and sort it.

Summary

  • Lists hold ordered, changeable items in [ ].
  • Methods: append, insert, remove, pop, sort, reverse.
  • sum/max/min/len work directly; in checks membership.

List क्या है?

List कई values को एक variable में, क्रम में, square brackets [ ] के अंदर रखती है। Lists mutable हैं — items जोड़, हटा और बदल सकते हैं।

Python – list.py
marks = [85, 92, 78, 90]
print(marks)
print(len(marks))   # कितने items
[85, 92, 78, 90] 4

Access और Slice

Python – access.py
fruits = ["apple", "banana", "mango"]
print(fruits[0])     # पहला
print(fruits[-1])    # आखिरी
print(fruits[0:2])   # पहले दो
apple mango ['apple', 'banana']

List Methods

Methodक्या करता है
.append(x)अंत में x जोड़ें
.insert(i,x)position i पर x जोड़ें
.remove(x)पहला x हटाएं
.pop()आखिरी हटाकर लौटाएं
.sort()जगह पर sort
.reverse()क्रम उल्टा
.count(x)कितने x

Program 1: बनाएं और Loop करें

Python – loop_list.py
names = ["Aman", "Riya", "Karan"]
for name in names:
    print("Student:", name)
Student: Aman Student: Riya Student: Karan

for loop हर item एक-एक करके लेता है — index की ज़रूरत नहीं।

Program 2: Sum, Max और Min

Python – stats.py
marks = [85, 92, 78, 90, 88]
print("Total  :", sum(marks))
print("Highest:", max(marks))
print("Lowest :", min(marks))
print("Average:", sum(marks) / len(marks))
Total : 433 Highest: 92 Lowest : 78 Average: 86.6

Built-in functions sum(), max(), min(), len() lists पर सीधे चलते हैं।

Program 3: List में Search

Python – search.py
numbers = [10, 25, 30, 45]
x = int(input("Search for: "))
if x in numbers:
    print(x, "found at position", numbers.index(x))
else:
    print(x, "not in list")
Search for: 30 30 found at position 2
  • x in numbers membership check करता है।
  • .index(x) item की position देता है।

Program 4: Sort और Duplicates हटाएं

Python – cleanup.py
data = [5, 2, 8, 2, 5, 1]
data = list(set(data))   # set duplicates हटाता है
data.sort()              # फिर sort
print(data)
[1, 2, 5, 8]

set(data) duplicates हटाता है; list() वापस list बनाता है; .sort() क्रम में लगाता है।

List Comprehension (झलक)

एक line में lists बनाने का छोटा तरीका:

Python – comp.py
squares = [x*x for x in range(1, 6)]
print(squares)
[1, 4, 9, 16, 25]

सामान्य गलतियाँ

  • Index out of range — 4-item list पर marks[10] error देता है।
  • .remove(x) error देता है अगर x मौजूद नहीं।
  • append (एक item) और extend (कई items) में confusion।

Practice Tasks

  1. 5 marks store करके total और average print करें।
  2. max() use किए बिना list में सबसे बड़ा number ढूंढें।
  3. List में even और odd numbers गिनें।
  4. List से duplicates हटाकर sort करें।

सारांश

  • Lists [ ] में क्रमबद्ध, बदलने योग्य items रखती हैं।
  • Methods: append, insert, remove, pop, sort, reverse।
  • sum/max/min/len सीधे चलते हैं; in membership check करता है।
← Back to Python Tutorial
🔗

Share this topic with a friend

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

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

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

WhatsApp