Introduction
A list is used to store multiple values in a single variable. Instead of creating separate variables for each value, we can store all the values together in a list.
What Is a List in Python?
A list is a collection of elements stored in a single variable.
Lists are:
- Ordered: Elements maintain their insertion order.
- Mutable: We can add, remove, or modify elements.
- Allow duplicates: The same value can appear more than once.
- Allow different data types: A list can contain numbers, strings, and other data types.
How to Create a List in Python?
In Python, lists are created using square brackets []. Elements are separated by commas.
Example:
fruits = ["Apple", "Banana", "Mango"]
print(fruits)
Output:
['Apple', 'Banana', 'Mango']
We can also create a list with different data types.
my_list = [10, "Python", 25.5, True]
print(my_list)
Output:
[10, 'Python', 25.5, True]
List Indexing in Python
Indexing is used to access individual elements from a list.
In Python, indexing starts from 0.
There are two types of indexing:
- Positive Indexing: Starts from the beginning (0, 1, 2, 3...).
- Negative Indexing: Starts from the end (-1, -2, -3...).
Example:
fruits = ["Apple", "Banana", "Mango", "Orange"]
print(fruits[0])
print(fruits[1])
print(fruits[-1])
print(fruits[-2])
Output:
Apple
Banana
Orange
Mango
List Slicing in Python
List slicing is used to access a specific portion of a list.
Syntax:
list[start:end:step]
-
start: Starting index. -
end: Ending index (not included). -
step: Number of positions to move.
Example:
numbers = [10, 20, 30, 40, 50, 60]
print(numbers[0:3])
print(numbers[2:5])
print(numbers[:4])
print(numbers[::2])
Output:
[10, 20, 30]
[30, 40, 50]
[10, 20, 30, 40]
[10, 30, 50]
List Operations in Python
Python supports different operations on lists.
1. Concatenation
Concatenation is used to combine two or more lists using the + operator.
Example:
list1 = [10, 20, 30]
list2 = [40, 50, 60]
result = list1 + list2
print(result)
Output:
[10, 20, 30, 40, 50, 60]
2. Repetition
The * operator is used to repeat the elements of a list.
Example:
numbers = [1, 2, 3]
print(numbers * 2)
Output:
[1, 2, 3, 1, 2, 3]
3. Membership
Membership operators are used to check whether an element exists in a list.
The operators are:
innot in
Example:
fruits = ["Apple", "Banana", "Mango"]
print("Apple" in fruits)
print("Orange" not in fruits)
Output:
True
True
Common List Methods in Python
Python provides several built-in methods to perform different operations on lists.
| Method | Description |
|---|---|
| append() | Adds an element at the end of the list. |
| insert() | Adds an element at a specified position. |
| extend() | Adds multiple elements from another iterable. |
| remove() | Removes the specified element. |
| pop() | Removes and returns an element. |
| clear() | Removes all elements from the list. |
| sort() | Sorts the list. |
| reverse() | Reverses the order of elements. |
| index() | Returns the index of an element. |
| count() | Counts how many times an element appears. |
| copy() | Creates a shallow copy of the list. |
Example of List Methods
fruits = ["Apple", "Banana", "Mango"]
fruits.append("Orange")
print(fruits)
fruits.insert(1, "Grapes")
print(fruits)
fruits.remove("Banana")
print(fruits)
fruits.pop()
print(fruits)
Output:
['Apple', 'Banana', 'Mango', 'Orange']
['Apple', 'Grapes', 'Banana', 'Mango', 'Orange']
['Apple', 'Grapes', 'Mango', 'Orange']
['Apple', 'Grapes', 'Mango']
Updating Elements in a List
Since lists are mutable, we can change their elements after creating them.
Example:
fruits = ["Apple", "Banana", "Mango"]
fruits[1] = "Orange"
print(fruits)
Output:
['Apple', 'Orange', 'Mango']
Here, the element at index 1 is changed from Banana to Orange.
Iterating Through a List Using a For Loop
We can use a for loop to access each element in a list one by one.
Example:
fruits = ["Apple", "Banana", "Mango"]
for fruit in fruits:
print(fruit)
Output:
Apple
Banana
Mango
Nested Lists in Python
A nested list is a list that contains another list as an element.
Example:
numbers = [[10, 20], [30, 40], [50, 60]]
print(numbers)
print(numbers[0])
print(numbers[1][0])
Output:
[[10, 20], [30, 40], [50, 60]]
[10, 20]
30
Here, numbers[1][0] accesses the first element of the second inner list.
Why Are Lists Important in Python?
Lists are important because:
- They allow us to store multiple values in one variable.
- They make it easy to add, remove, and update elements.
- They help organize and manage data.
- They are useful for storing collections of information.
- They make data processing easier.
Top comments (0)