Python Foundation
Lists in Python
What is a List?
A list is an ordered and mutable collection of items in Python.
Lists can store multiple items in a single variable and the items can be of different data types.
Python
fruits = ["apple", "banana", "mango"]
numbers = [10, 20, 30, 40]
mixed = ["Kundan", 25, 5.9, True]
print(fruits)
print(type(fruits))
Output
['apple', 'banana', 'mango'] <class 'list'>
Key Characteristics
- Ordered → Items have a defined order
- Mutable → You can change, add, or remove items
- Allows Duplicates
- Can store different data types
Creating Lists
Using Square Brackets
Python
marks = [85, 90, 78, 92]
names = ["Ravi", "Priya", "Kiran"]
empty_list = []
print(marks)
print(names)
Using list() Constructor
Python
numbers = list((10, 20, 30))
print(numbers)
chars = list("Python")
print(chars)
Output
[10, 20, 30] ['P', 'y', 't', 'h', 'o', 'n']
Indexing and Slicing
Lists support indexing and slicing just like strings.
Python
fruits = ["apple", "banana", "mango", "orange", "grape"]
print(fruits[0]) # First item
print(fruits[-1]) # Last item
print(fruits[1:4]) # Slicing
print(fruits[:3]) # From start
print(fruits[2:]) # Till end
print(fruits[::-1]) # Reverse
Output
apple grape ['banana', 'mango', 'orange'] ['apple', 'banana', 'mango'] ['mango', 'orange', 'grape'] ['grape', 'orange', 'mango', 'banana', 'apple']
Changing List Items
Since lists are mutable, you can change items using their index.
Python
colors = ["red", "green", "blue"]
print("Before:", colors)
colors[1] = "yellow"
print("After:", colors)
# Change a range of items
colors[0:2] = ["black", "white"]
print("Updated:", colors)
Output
Before: ['red', 'green', 'blue'] After: ['red', 'yellow', 'blue'] Updated: ['black', 'white', 'blue']
Adding Items to a List
append() – Add at the end
Python
nums = [10, 20, 30]
nums.append(40)
print(nums)
Output
[10, 20, 30, 40]
insert() – Add at specific position
Python
nums.insert(1, 15)
print(nums)
Output
[10, 15, 20, 30, 40]
extend() – Add multiple items
Python
nums.extend([50, 60])
print(nums)
Output
[10, 15, 20, 30, 40, 50, 60]
Removing Items from a List
Python
fruits = ["apple", "banana", "mango", "orange", "banana"]
fruits.remove("banana") # Removes first matching item
print(fruits)
fruits.pop(1) # Removes item at index 1
print(fruits)
fruits.pop() # Removes last item
print(fruits)
del fruits[0] # Delete using index
print(fruits)
fruits.clear() # Remove all items
print(fruits)
Output
['apple', 'mango', 'orange', 'banana'] ['apple', 'orange', 'banana'] ['apple', 'orange'] ['orange'] []
Important List Methods
Python
numbers = [5, 2, 9, 1, 5, 6]
print(len(numbers)) # Length
print(numbers.count(5)) # Count occurrences
print(numbers.index(9)) # Find index
numbers.sort() # Sort ascending
print(numbers)
numbers.reverse() # Reverse the list
print(numbers)
copied = numbers.copy() # Create a copy
print(copied)
Output
6 2 2 [1, 2, 5, 5, 6, 9] [9, 6, 5, 5, 2, 1] [9, 6, 5, 5, 2, 1]
Looping Through a List
Using for loop
Python
fruits = ["apple", "banana", "mango"]
for fruit in fruits:
print(fruit)
Output
apple banana mango
Using index
Python
for i in range(len(fruits)):
print(i, fruits[i])
Output
0 apple 1 banana 2 mango
Using enumerate()
Python
for index, fruit in enumerate(fruits):
print(index, fruit)
Summary – Lists in Python
- List is an ordered and mutable collection.
- Created using square brackets
[]. - Supports indexing and slicing.
- Common methods:
append(),insert(),extend(),remove(),pop(),sort(),reverse(),count(),index() - You can loop through a list using
forloop.
Next Topic → Tuples in Python
Lists in Python
Learn how to create, access, modify and manage ordered collections of data.
Select any topic from the left sidebar to begin.
Start Learning →
