What is a List?
A List is a collection of multiple elements stored in a single variable.
Lists are written using square brackets [].
numbers = [1, 2, 3, 4, 5]
print(numbers)
Output:
[1, 2, 3, 4, 5]
Creating a List
- There are different ways to create a list.
Creating an Empty List
- We can create an empty list using [].
numbers = []
print(numbers)
numbers = list()
print(numbers)
Output:
[]
Creating a List with Values
- We can directly provide values inside square brackets.
numbers = [1, 2, 3, 4, 5]
print(numbers)
Output:
[1, 2, 3, 4, 5]
List with Different Data Types
- A Python list can contain different types of values.
numbers = [1, 2, 3, "a", "b", "c"]
print(numbers)
Output:
[1, 2, 3, 'a', 'b', 'c']
List Indexing
Indexing is used to access individual elements from a list.
Python uses zero-based indexing.
numbers = [10, 20, 30, 40, 50]
Output:
Value: 10 20 30 40 50
Index: 0 1 2 3 4
Access the first element
print(numbers[0])
Output:
10
Looping Through a List
- We can use a for loop to access every element in a list.
numbers = [1, 2, 3, 4, 5]
for i in numbers:
print(i)
Output:
1
2
3
4
5
List Slicing
- Slicing is used to get a portion of a list.
Syntax
list[start:stop:step]
Example
numbers = [1, 2, 3, 4, 5]
Basic Slicing
print(numbers[1:4])
Output:
[2, 3, 4]
- Start = 1
- Stop = 4
- Index 4 is not included
Slicing with Step
print(numbers[1:4:1])
Output:
[2, 3, 4]
Slicing from the Beginning
print(numbers[:4])
Output:
[1, 2, 3, 4]
Finding List Methods
- Python provides many built-in methods for lists.
- dir()
- append()
- insert()
- extend()
- pop()
- clear()
- reverse()
- sort()
append() – Add an Element
- append() adds one element to the end of a list.
numbers = [1, 2, 3, 4]
numbers.append(5)
print(numbers)
Output:
[1, 2, 3, 4, 5]
insert() – Add at a Specific Position
- insert() adds an element at a specific index.
Syntax:
list.insert(index, value)
numbers = [1, 2, 3, 4]
numbers.insert(0, 10)
print(numbers)
Output:
[10, 1, 2, 3, 4]
extend() – Add Multiple Elements
- extend() adds multiple elements to the end of a list.
numbers = [1, 2, 3, 4]
numbers.extend([6, 7, 8, 9])
print(numbers)
Output:
[1, 2, 3, 4, 6, 7, 8, 9]
pop() – Remove an Element
- pop() removes an element from a list.
numbers = [1, 2, 3, 4]
numbers.pop()
print(numbers)
Output:
[1, 2, 3]
pop(index)
numbers = [1, 2, 3, 4]
numbers.pop(0)
print(numbers)
Output:
[2, 3, 4]
clear() – Remove All Elements
- clear() removes all elements from the list.
numbers = [1, 2, 3, 4]
numbers.clear()
print(numbers)
Output:
[]
reverse() – Reverse a List
- reverse() changes the order of elements.
numbers = [1, 2, 3, 4]
numbers.reverse()
print(numbers)
Output:
[4, 3, 2, 1]
sort() – Sort a List
- sort() arranges the elements in ascending order.
numbers = [5, 2, 8, 1, 3]
numbers.sort()
print(numbers)
Output:
[1, 2, 3, 5, 8]
Descending Order
numbers.sort(reverse=True)
Example:
numbers = [5, 2, 8, 1, 3]
numbers.sort(reverse=True)
print(numbers)
Output:
[8, 5, 3, 2, 1]
Finding the Sum of a List
- Python provides the sum() function to calculate the total of numeric values.
numbers = [1, 2, 3, 4]
total = sum(numbers)
print(total)
Output:
10
Updating List Elements
- Lists are modifiable, so we can change individual elements.
Example:
numbers = [1, 2, 3, 4, 5]
numbers[0] = 0
print(numbers)
Output:
[0, 2, 3, 4, 5]
Finding the Length of a List
- The len() function returns the number of elements in a list.
numbers = [1, 2, 3, 4, 5]
length = len(numbers)
print(length)
Output:
5
Performing Calculations on List Elements
- We can use a loop to perform calculations on every element.
numbers = [1, 2, 3, 4, 5]
length = len(numbers)
for i in range(length):
numbers[i] = numbers[i] ** 2
print(numbers)
Output:
[1, 4, 9, 16, 25]
Important List Methods
Method / Function Purpose
append() Adds an element at the end
insert() Adds an element at a specific position
extend() Adds multiple elements
pop() Removes an element
clear() Removes all elements
reverse() Reverses the list
sort() Sorts the list
sum() Calculates the total
len() Finds the number of elements
dir() Shows available attributes/methods
Top comments (0)