What is a list?#
A list stores many values in a single variable, in order. It is the most used data structure in Python.
marks = [78, 85, 92, 66]
names = ["Asha", "Ravi", "Neha"]
mixed = [1, "two", 3.0, True, None] # different types allowed
empty = []
Lists are:
- Ordered - items keep their position.
- Mutable - you can add, change and remove items.
- Allow duplicates - the same value can appear many times.
- Indexed - from 0, just like strings.
List को एक रजिस्टर या सूची की तरह समझें जिसमें कई चीज़ें क्रम से लिखी होती हैं। एक ही variable में आप 100 students के marks रख सकते हैं, बजाय 100 अलग variables बनाने के।
List की खास बातें: इसमें क्रम (order) बना रहता है, एक जैसी values दोबारा आ सकती हैं, और आप इसे बदल सकते हैं (mutable) - नई चीज़ जोड़ना, हटाना, बदलना सब संभव है।
Accessing items#
Changing items#
Adding items#
| Method | What it does | Example |
|---|---|---|
append(x) |
add one item at the end | nums.append(5) |
insert(i, x) |
add at position i |
nums.insert(0, 99) |
extend(iterable) |
add each item of another list | nums.extend([6, 7]) |
+ |
join two lists into a new list | a + b |
cart.append(["eraser", "scale"]) would add the list as a single item: [..., ['eraser', 'scale']]. Use extend to add the items one by one.
Removing items#
| Method / statement | What it does |
|---|---|
remove(x) |
removes the first item equal to x (ValueError if not found) |
pop() |
removes and returns the last item |
pop(i) |
removes and returns the item at index i |
del lst[i] |
deletes the item at index i (or a slice) |
clear() |
removes everything |
Searching and counting#
Sorting and reversing#
list.sort() और sorted(list) में फर्क:
nums.sort()- इसी list को बदल देता है और कुछ return नहीं करता (Noneदेता है)।sorted(nums)- original list को नहीं छूता, एक नई sorted list देता है।
आम गलती: nums = nums.sort() लिखने पर nums में None आ जाता है!
Useful built-in functions#
Looping through lists#
Building a list in a loop#
(Later you'll learn a one-line way to do this: [n * n for n in range(1, 6)].)
Nested lists (2D lists)#
A list can contain other lists - useful for tables and matrices:
Copying a list correctly#
Assignment does not copy a list. Both names refer to the same list:
For nested lists, .copy() makes a shallow copy - the inner lists are still shared. Use import copy; copy.deepcopy(x) for a fully independent copy.
Unpacking#
point = [10, 20]
x, y = point
first, *rest = [1, 2, 3, 4]
print(first, rest) # 1 [2, 3, 4]
List methods - quick reference#
| Method | Description |
|---|---|
append(x) |
add item at end |
extend(it) |
add all items from iterable |
insert(i, x) |
insert at index |
remove(x) |
remove first matching value |
pop([i]) |
remove & return item |
clear() |
remove all |
index(x) |
position of first match |
count(x) |
number of matches |
sort() |
sort in place |
reverse() |
reverse in place |
copy() |
shallow copy |
Summary#
- Lists are ordered, mutable collections:
[a, b, c]. - Index and slice like strings; change items with
lst[i] = value. - Add:
append,insert,extend. Remove:remove,pop,del,clear. sort()modifies;sorted()returns a new list.b = ashares the same list - usea.copy()for a copy.