A list in Python is a mutable, ordered collection of items that can contain elements of different data types (integers, strings, objects, etc.).
Python provides several built-in list functions and methods that make it easy to perform operations such as adding, removing, sorting, and manipulating elements.
πΉ Common List Functions & Methods
| Function / Method | Description | Example |
|---|---|---|
len(list) | Returns the number of elements in the list. | len([1, 2, 3]) β 3 |
max(list) | Returns the largest element in the list. | max([1, 4, 2]) β 4 |
min(list) | Returns the smallest element in the list. | min([1, 4, 2]) β 1 |
sum(list) | Returns the sum of all numeric elements. | sum([1, 2, 3]) β 6 |
list.append(x) | Adds an item to the end of the list. | [1, 2].append(3) β [1, 2, 3] |
list.insert(i, x) | Inserts an element at a specific index. | [1, 3].insert(1, 2) β [1, 2, 3] |
list.extend(iterable) | Adds multiple elements from another list or iterable. | [1, 2].extend([3, 4]) β [1, 2, 3, 4] |
list.remove(x) | Removes the first occurrence of element x. | [1, 2, 2, 3].remove(2) β [1, 2, 3] |
list.pop(i) | Removes and returns element at index i (default: last element). | [1, 2, 3].pop() β 3 |
list.clear() | Removes all elements from the list. | [1, 2, 3].clear() β [] |
list.index(x) | Returns the index of the first occurrence of x. | [10, 20, 30].index(20) β 1 |
list.count(x) | Returns the number of times x appears. | [1, 2, 2, 3].count(2) β 2 |
list.sort() | Sorts the list in ascending order (by default). | [3, 1, 2].sort() β [1, 2, 3] |
list.reverse() | Reverses the elements of the list. | [1, 2, 3].reverse() β [3, 2, 1] |
list.copy() | Returns a shallow copy of the list. | a = [1, 2]; b = a.copy() |
π§© Example 1: Basic List Operations
fruits = ["apple", "banana", "cherry"]
fruits.append("orange")
fruits.insert(1, "mango")
print(fruits)Output:
['apple', 'mango', 'banana', 'cherry', 'orange']π§© Example 2: Removing and Popping Elements
numbers = [10, 20, 30, 40, 50]
numbers.remove(30)
popped = numbers.pop()
print("After remove:", numbers)
print("Popped element:", popped)Output:
After remove: [10, 20, 40]
Popped element: 50π§© Example 3: Sorting and Reversing
nums = [5, 2, 9, 1]
nums.sort()
print("Sorted:", nums)
nums.reverse()
print("Reversed:", nums)Output:
Sorted: [1, 2, 5, 9]
Reversed: [9, 5, 2, 1]π§© Example 4: Counting and Indexing
data = [1, 2, 2, 3, 4, 2]
print("Count of 2:", data.count(2))
print("Index of 3:", data.index(3))Output:
Count of 2: 3
Index of 3: 3π§© Example 5: Copying and Clearing a List
colors = ["red", "blue", "green"]
new_colors = colors.copy()
colors.clear()
print("Original after clear:", colors)
print("Copied list:", new_colors)Output:
Original after clear: []
Copied list: ['red', 'blue', 'green']πΉ In Summary
- Lists are mutable, ordered, and can hold mixed data types.
- Python provides various built-in functions and methods to manipulate lists efficiently.
- Common operations include adding, removing, sorting, counting, and copying elements.
- Lists are among the most flexible and widely used data structures in Python.
