🐍 List in Python
Understanding Python Lists with Examples
1. What is a List?
A List in Python is an ordered and mutable collection of elements.
A list can contain numbers, strings, Boolean values, or even other lists.
Lists are created using square brackets [ ].
my_list = [10, 20, 30, 40, 50]
print(my_list)
Output:
[10, 20, 30, 40, 50]
[10, 20, 30, 40, 50]
2. Creating a List
numbers = [10, 20, 30, 40]
names = ["Rahul", "Amit", "Priya"]
mixed = [10, "Python", 3.14, True]
empty = []
Output:
numbers → [10, 20, 30, 40]
names → ['Rahul', 'Amit', 'Priya']
mixed → [10, 'Python', 3.14, True]
empty → []
numbers → [10, 20, 30, 40]
names → ['Rahul', 'Amit', 'Priya']
mixed → [10, 'Python', 3.14, True]
empty → []
3. Important Properties of a List
| Property | Description |
|---|---|
| Ordered | Elements maintain their insertion order. |
| Mutable | Elements can be changed after creation. |
| Allows duplicates | The same value can appear multiple times. |
| Heterogeneous | A list can contain different data types. |
| Indexed | Every element has a position/index. |
4. List Indexing
Python uses zero-based indexing. Therefore, the first element has index 0.
fruits = ["Apple", "Mango", "Banana", "Orange"]
print(fruits[0])
print(fruits[2])
print(fruits[-1])
Output:
Apple
Banana
Orange
Apple
Banana
Orange
💡 Tip: Negative indexing starts from the end.
-1 represents the last element.
5. List Slicing
Slicing is used to extract a portion of a list.
numbers = [10, 20, 30, 40, 50]
print(numbers[1:4])
print(numbers[:3])
print(numbers[2:])
Output:
[20, 30, 40]
[10, 20, 30]
[30, 40, 50]
[20, 30, 40]
[10, 20, 30]
[30, 40, 50]
6. Changing List Elements
numbers = [10, 20, 30, 40]
numbers[1] = 200
print(numbers)
Output:
[10, 200, 30, 40]
[10, 200, 30, 40]
✅ Lists are mutable, so their elements can be modified
after the list has been created.
7. Adding Elements to a List
numbers = [10, 20, 30]
numbers.append(40)
print(numbers)
Output:
[10, 20, 30, 40]
[10, 20, 30, 40]
append() adds one element at the end of the list.
numbers = [10, 20, 30]
numbers.insert(1, 15)
print(numbers)
Output:
[10, 15, 20, 30]
[10, 15, 20, 30]
insert(index, value) adds an element at a specific position.
8. Removing Elements
numbers = [10, 20, 30, 40]
numbers.remove(30)
print(numbers)
Output:
[10, 20, 40]
[10, 20, 40]
numbers = [10, 20, 30, 40]
numbers.pop()
print(numbers)
Output:
[10, 20, 30]
[10, 20, 30]
9. Important List Methods
| Method | Purpose |
|---|---|
| append() | Adds an element at the end. |
| insert() | Adds an element at a specified position. |
| remove() | Removes a specified value. |
| pop() | Removes an element using its index. |
| clear() | Removes all elements. |
| sort() | Sorts the list. |
| reverse() | Reverses the list. |
| count() | Counts occurrences of a value. |
| index() | Returns the index of a value. |
10. Sorting a List
numbers = [50, 10, 40, 20, 30]
numbers.sort()
print(numbers)
Output:
[10, 20, 30, 40, 50]
[10, 20, 30, 40, 50]
numbers.sort(reverse=True)
print(numbers)
Output:
[50, 40, 30, 20, 10]
[50, 40, 30, 20, 10]
11. Traversing a List
numbers = [10, 20, 30, 40, 50]
for n in numbers:
print(n)
Output:
10
20
30
40
50
10
20
30
40
50
12. List Length
numbers = [10, 20, 30, 40, 50]
print(len(numbers))
Output:
5
5
13. List Comprehension
List comprehension provides a short and elegant way to create a
new list from an existing iterable.
squares = [x * x for x in range(1, 6)]
print(squares)
Output:
[1, 4, 9, 16, 25]
[1, 4, 9, 16, 25]
14. Example: Student Marks
marks = [75, 82, 68, 91, 88]
total = sum(marks)
average = total / len(marks)
print("Total =", total)
print("Average =", average)
Output:
Total = 404
Average = 80.8
Total = 404
Average = 80.8
15. Quick Revision
✔ List is ordered
✔ List is mutable
✔ List uses [ ] brackets
✔ Index starts from 0
✔ Negative indexing is supported
✔ Duplicate values are allowed
✔ Different data types can be stored
✔ List supports slicing
✔ Lists provide many built-in methods
✔ List is mutable
✔ List uses [ ] brackets
✔ Index starts from 0
✔ Negative indexing is supported
✔ Duplicate values are allowed
✔ Different data types can be stored
✔ List supports slicing
✔ Lists provide many built-in methods
No comments:
Post a Comment