Lists in Python
๐ข Beginner
๐ Definition
A List in Python is an ordered, mutable (modifiable) collection of elements enclosed in square brackets []. Lists can store elements of mixed data types (integers, strings, floats, nested lists) and allow duplicate values.
๐ฎ๐ณ Hindi
List Python ki sabse popular mutable ordered collection hai. List mein elements square brackets [] ke andar hote hain. Indexing 0 se start hoti hai, aur last element ke liye negative indexing -1 ka use kiya jata hai.
๐ฉ Marathi
List mutable ahe (tyat badal karta yeto). Indexing 0 pasun suru hote aani shevatcha item ghenyasathi -1 Indexing vaparatat.
๐ 1. Indexing & Slicing (list[start:stop:step])
- Zero-Based Indexing: First item
list[0], second itemlist[1]. - Negative Indexing: Last item
list[-1], second to lastlist[-2]. - Slicing (
list[start:stop:step]): Extracts a sub-list fromstartup to (but excluding)stop.
colors = ["Red", "Green", "Blue", "Yellow", "Orange"]
print(colors[0]) # "Red"
print(colors[-1]) # "Orange"
print(colors[1:4]) # ["Green", "Blue", "Yellow"] (index 1, 2, 3)
print(colors[::-1]) # Reverses list: ["Orange", "Yellow", "Blue", "Green", "Red"]
๐ 2. Essential List Methods
| Method | What It Does | Mutates List? |
|---|---|---|
.append(item) | Adds single item to the end | โ Yes |
.extend(iterable) | Appends all elements from another list | โ Yes |
.insert(index, item) | Inserts item at specific index | โ Yes |
.pop(index) | Removes & returns item at index (default last) | โ Yes |
.remove(item) | Removes first occurrence of matching item | โ Yes |
.sort(reverse=True) | Sorts elements in-place | โ Yes |
.reverse() | Reverses elements in-place | โ Yes |
.copy() | Creates a shallow copy of the list | โ No |
๐ง 3. Aliasing (b = a) vs Shallow Copying (b = a.copy())
โ ๏ธ CRITICAL PITFALL: Writing
list_b = list_adoes NOT copy the list! It creates two variable names pointing to the same underlying list in memory. Modifyinglist_bWILL modifylist_a!
a = [1, 2, 3]
b = a # Alias (Same reference in memory!)
c = a.copy() # Shallow Copy (Independent new list!)
b.append(99)
print("a:", a) # [1, 2, 3, 99] (Modified!)
print("c:", c) # [1, 2, 3] (Unchanged!)
๐ก Complete Example: Student Score Analytics
# Student Test Score Analytics
scores = [78, 92, 45, 88, 63, 95, 52]
print("=== STUDENT SCORE ANALYTICS ===")
print("Original Scores :", scores)
1. Modifying Elements
scores.append(82) # Adding new score to end
scores.insert(0, 90) # Inserting score at index 0
scores.remove(45) # Removing failing score 45
2. Statistical Analysis
total_students = len(scores)
highest_score = max(scores)
lowest_score = min(scores)
average_score = sum(scores) / total_students
3. Sorting Scores (Non-destructive copy using sorted())
sorted_scores = sorted(scores, reverse=True)
print(f"Updated Scores : {scores}")
print(f"Total Students : {total_students}")
print(f"Highest Score : {highest_score}")
print(f"Lowest Score : {lowest_score}")
print(f"Average Score : {average_score:.2f}")
print(f"Sorted Top-Down : {sorted_scores}")
๐ Output
=== STUDENT SCORE ANALYTICS ===
Original Scores : [78, 92, 45, 88, 63, 95, 52]
Updated Scores : [90, 78, 92, 88, 63, 95, 52, 82]
Total Students : 8
Highest Score : 95
Lowest Score : 52
Average Score : 78.75
Sorted Top-Down : [95, 92, 90, 88, 82, 78, 63, 52]
โ ๏ธ Common Mistakes
IndexError: list index out of range: Attempting to access an index that doesn't exist (e.g.,list[len(list)]).- Calling
.sort()or.append()and expecting it to return the new list (result = my_list.sort()setsresulttoNonebecause.sort()modifies the list in-place and returnsNone!).
๐ก๏ธ Safety / Important Notes
If a list contains nested mutable objects (e.g. lists inside lists [[1, 2], [3, 4]]), use copy.deepcopy() to create a fully independent recursive copy.
๐ Real-World Usage
Shopping cart items, search result sets, message queues, time-series data, and batch record processing.
๐งช Try It Yourself
1. Create a list of 5 product prices [120, 450, 80, 990, 230].
2. Add a new price to the end using .append() and remove the lowest price using .remove(min(prices)).
๐ฏ Mini Challenge
Write a program that takes a list with duplicate numbers [10, 20, 10, 30, 20, 40] and creates a new list with duplicates removed while preserving the original element order.
๐ Related Topics
๐งญ Navigation
โ Python Home | โ Previous: Functions | Next: Tuples โ