Here is a clear, complete, and exam-friendly explanation of Lists in Python, perfect for BCA/MCA/B.Tech students.
Lists in Python
A list in Python is an ordered, mutable (changeable) collection of items.
Lists can store different types of data such as integers, strings, floats, or even other lists.
Lists are one of the most powerful and commonly used data types in Python.
1. Creating a List
Lists are created using square brackets [ ].
Examples:
numbers = [10, 20, 30, 40]
fruits = ["apple", "banana", "mango"]
mixed = [10, "Python", 3.14, True]
nested = [1, 2, [3, 4], 5] # List inside list
2. Accessing List Elements
Use indexing.
Python uses zero-based indexing.
Example:
fruits = ["apple", "banana", "mango"]
print(fruits[0]) # apple
print(fruits[1]) # banana
print(fruits[2]) # mango
Negative indexing:
print(fruits[-1]) # mango
print(fruits[-2]) # banana
3. List Slicing
Used to access a range of elements.
Syntax:
list[start : end : step]
Examples:
nums = [1, 2, 3, 4, 5, 6]
print(nums[1:4]) # [2, 3, 4]
print(nums[:3]) # [1, 2, 3]
print(nums[3:]) # [4, 5, 6]
print(nums[::2]) # [1, 3, 5]
4. Modifying Lists (Mutable)
Lists are mutable, meaning their elements can be changed.
Changing a value:
nums = [1, 2, 3]
nums[1] = 20
print(nums) # [1, 20, 3]
Adding elements:
Using append():
nums.append(4)
Using insert():
nums.insert(1, 15)
5. Removing Elements
remove(value) โ removes first occurrence
nums.remove(20)
pop(index) โ removes and returns item
nums.pop(1)
del โ delete by index or entire list
del nums[0]
6. List Methods
Python provides many useful list methods.
| Method | Description |
|---|---|
append(x) | Add element at end |
extend(iterable) | Add multiple elements |
insert(i, x) | Insert at index |
remove(x) | Remove item |
pop(i) | Remove and return item |
clear() | Remove all elements |
sort() | Sort list |
reverse() | Reverse list |
index(x) | Return index of item |
count(x) | Count occurrences |
7. Using Lists in Loops
Example:
fruits = ["apple", "banana", "mango"]
for f in fruits:
print(f)
8. List Length
Use len():
len([10, 20, 30]) # 3
9. List Comprehension
A simple and elegant way to create lists.
Example:
squares = [x*x for x in range(1, 6)]
print(squares)
Output:
[1, 4, 9, 16, 25]
10. Nested Lists
List inside another list.
matrix = [
[1, 2],
[3, 4],
[5, 6]
]
print(matrix[1][0]) # 3
11. Checking Membership
fruits = ["apple", "banana"]
print("apple" in fruits) # True
print("orange" not in fruits) # True
12. Copying Lists
Shallow copy:
new_list = old_list.copy()
Using slicing:
new_list = old_list[:]
13. Joining Lists
Using +:
a = [1, 2]
b = [3, 4]
c = a + b
Using extend():
a.extend(b)
14. Important Points About Lists
- Lists can grow dynamically.
- They are ordered.
- They allow duplicate values.
- They are mutable.
- They can contain mixed data types.
15. Examples for Practice
Example 1: Largest number in list
nums = [10, 5, 20, 3]
print(max(nums))
Example 2: Sum of list
print(sum(nums))
Example 3: Remove all even numbers
nums = [1, 2, 3, 4, 5, 6]
odd = [x for x in nums if x % 2 != 0]
print(odd)
Summary Table: Lists in Python
| Feature | Description |
|---|---|
| Type | Sequence |
| Mutable | Yes |
| Ordered | Yes |
| Allows duplicates | Yes |
| Syntax | [ ] |
| Access | Indexing & slicing |
