Python Lists
A List is one of the most important data structures in Python. Lists are used to store multiple values in a single variable.
Why Lists are Important?
- Lists store multiple values together.
- Lists help organize data.
- Lists are used in loops, databases, APIs, and applications.
- Lists are mutable, meaning values can be changed.
- Lists support indexing and slicing.
Creating a List
Lists are created using square brackets [].
numbers = [10, 20, 30, 40] print(numbers)
Output
[10, 20, 30, 40]
Here:
- numbers is the list name.
- 10, 20, 30, and 40 are list elements.
- Elements are separated using commas.
Lists can Store Different Data Types
data = [10, "Python", 99.5, True] print(data)
Output
[10, 'Python', 99.5, True]
Python lists can store integers, strings, floats, and boolean values together.
List Indexing
Each element in a list has an index number. Indexing starts from 0.
fruits = ["Apple", "Mango", "Orange"] print(fruits[0]) print(fruits[1]) print(fruits[2])
Output
Apple Mango Orange
Explanation:
- Apple is at index 0
- Mango is at index 1
- Orange is at index 2
Negative Indexing
Negative indexing starts from the end of the list.
fruits = ["Apple", "Mango", "Orange"] print(fruits[-1]) print(fruits[-2])
Output
Orange Mango
List Slicing
Slicing is used to get multiple elements from a list.
numbers = [10, 20, 30, 40, 50] print(numbers[1:4])
Python starts from index 1 and stops before index 4.
Output
[20, 30, 40]
Changing List Values
Lists are mutable, meaning elements can be modified.
fruits = ["Apple", "Mango", "Orange"] fruits[1] = "Banana" print(fruits)
Output
['Apple', 'Banana', 'Orange']
Adding Elements using append()
append() adds a new element at the end of the list.
numbers = [10, 20, 30] numbers.append(40) print(numbers)
Output
[10, 20, 30, 40]
Adding Elements using insert()
insert() adds an element at a specific position.
numbers = [10, 20, 30] numbers.insert(1, 15) print(numbers)
Output
[10, 15, 20, 30]
Removing Elements using remove()
remove() deletes a specific element from the list.
fruits = ["Apple", "Mango", "Orange"]
fruits.remove("Mango")
print(fruits)
Output
['Apple', 'Orange']
Removing Elements using pop()
pop() removes element using index number.
numbers = [10, 20, 30] numbers.pop(1) print(numbers)
Output
[10, 30]
Finding List Length
Use len() function to count total elements.
numbers = [10, 20, 30, 40] print(len(numbers))
Output
4
Looping Through a List
Lists are commonly used with loops.
fruits = ["Apple", "Mango", "Orange"]
for item in fruits:
print(item)
Output
Apple Mango Orange
Checking if Item Exists
Use the in keyword to check elements.
fruits = ["Apple", "Mango", "Orange"]
print("Mango" in fruits)
print("Banana" in fruits)
Output
True False
Sorting a List
sort() arranges elements in ascending order.
numbers = [50, 10, 40, 20] numbers.sort() print(numbers)
Output
[10, 20, 40, 50]
Reversing a List
numbers = [10, 20, 30] numbers.reverse() print(numbers)
Output
[30, 20, 10]
Copying a List
numbers = [10, 20, 30] new_list = numbers.copy() print(new_list)
Output
[10, 20, 30]
Nested Lists
A list inside another list is called nested list.
data = [
[1, 2],
[3, 4]
]
print(data)
Output
[[1, 2], [3, 4]]
Real Life Example
This example stores student marks in a list.
marks = [85, 90, 78, 92] total = sum(marks) print(total) average = total / len(marks) print(average)
Output
345 86.25
Summary
- Lists store multiple values.
- Lists are mutable.
- Lists support indexing and slicing.
- append() adds elements.
- remove() deletes elements.
- sort() arranges values.
- Lists work well with loops.