List Methods
Python lists come with a rich set of built-in methods that allow you to add, remove, search, modify, and organize data efficiently.
Below is a complete and accurate table of all list methods.
All Python List Methods (Table)
| Method | Description |
|---|---|
append(x) | Adds an item to the end of the list |
extend(iterable) | Adds elements of an iterable to the list |
insert(i, x) | Inserts an item at a specific index |
remove(x) | Removes the first occurrence of a value |
pop(i) | Removes and returns item at index (default last) |
clear() | Removes all items from the list |
index(x) | Returns index of first occurrence of value |
count(x) | Returns number of times value appears |
sort() | Sorts the list |
reverse() | Reverses the order of the list |
copy() | Returns a shallow copy of the list |
Python Collections (Arrays)
Python does not have traditional arrays like some other languages.
Instead, lists are used as dynamic arrays.
Why lists are used as arrays:
- Can store multiple values
- Can grow or shrink dynamically
- Support indexing and slicing
- Can store mixed data types
python
numbers = [10, 20, 30, 40]
print(numbers)
python
numbers[0] = 100
print(numbers)
The list() Constructor
The
list() constructor is used to create a list from another iterable.From Tuple
python
data = list((1, 2, 3))
print(data)
From String
python
letters = list("Python")
print(letters)
From Range
python
numbers = list(range(5))
print(numbers)
Using type() with Lists
The
type() function is used to check the data type of a variable.python
x = [1, 2, 3]
print(type(x))
python
print(type([]))
List Items – Data Types
A list can store multiple data types in a single list.
Same Data Type
python
numbers = [1, 2, 3, 4]
print(numbers)
Mixed Data Types
python
data = ["Python", 10, 3.14, True]
print(data)
Nested Lists
python
matrix = [[1, 2], [3, 4], [5, 6]]
print(matrix)
python
print(matrix[0])
print(matrix[1][1])
Important Characteristics of List Items
- List items are ordered
- Indexing starts from
0 - Supports negative indexing
- Allows duplicate values
python
values = [10, 20, 20, 30]
print(values)
Summary
- Lists are Python’s built-in dynamic arrays
- Python provides many built-in list methods
list()constructor creates lists from iterablestype()confirms a variable is a list- Lists can store same or mixed data types
- Nested lists are supported
This topic forms the foundation for advanced data structures like stacks, queues, and matrices.