- Introduction to Lists
- 🛠️ Common List Methods
- 🔄 Checking List Equality
- 📊 List Indexing and Slicing
- ➕ Concatenation and Modification
- 🧩 Nested Lists
- ✏️ Modifying List Values
- ➕ Python List Operations
- 🔄 Iterating Through a List
- ➕ Adding Elements to a List
- ❌ Removing Elements from a List
- ⚙️ Modifying and Deleting List Items
- 🔀 Sorting a List
A list in Python is a versatile and mutable data structure used to store a sequence of various types of data. Lists allow for dynamic and flexible manipulation of data, making them one of the most commonly used data types in Python.
A list is a collection of items (elements) that are ordered and mutable (i.e., you can change the items in the list). Lists are created by placing the items inside square brackets []
, separated by commas.
L1 = ["John", 102, "USA"]
L2 = [1, 2, 3, 4, 5, 6]
print(type(L1)) # Output: <class 'list'>
print(type(L2)) # Output: <class 'list'>
- Ordered: Lists maintain the order of elements.
- Mutable: Items in a list can be changed.
- Flexible: Lists can store elements of different types.
Python lists come with a variety of methods that allow for a wide range of operations, such as adding, removing, and sorting elements. Let's dive into each method with detailed explanations and examples.
- Description: Appends an item
x
to the end of the list. - Use Case: Useful when you want to add a single item to a list without modifying any of the existing elements.
cubes = [1, 8, 27, 65, 125]
cubes.append(216) # Adds 216 to the end of the list
print(cubes) # Output: [1, 8, 27, 65, 125, 216]
- Description: Removes all items from the list, resulting in an empty list.
- Use Case: When you need to reset a list and remove all its elements.
lst = [1, 2, 3, 4, 5]
lst.clear()
print(lst) # Output: []
- Description: Returns a shallow copy of the list.
- Use Case: When you need a copy of a list that is independent of the original list.
original_list = [1, 2, 3]
copied_list = original_list.copy()
copied_list.append(4)
print(original_list) # Output: [1, 2, 3]
print(copied_list) # Output: [1, 2, 3, 4]
- Description: Returns the number of times the item
x
appears in the list. - Use Case: Useful for counting occurrences of an element in a list.
lst = [1, 2, 2, 3, 2, 4]
print(lst.count(2)) # Output: 3
- Description: Extends the list by appending all the items from the
iterable
(e.g., another list, tuple). - Use Case: To combine multiple lists or add elements from an iterable to the current list.
lst1 = [1, 2, 3]
lst2 = [4, 5, 6]
lst1.extend(lst2)
print(lst1) # Output: [1, 2, 3, 4, 5, 6]
- Description: Returns the index of the first occurrence of item
x
. Optionalstart
andend
arguments define a range to search within the list. - Use Case: To find the position of an item in the list.
lst = [1, 2, 3, 4, 2]
print(lst.index(2)) # Output: 1
print(lst.index(2, 2)) # Output: 4 (search starts at index 2)
- Description: Inserts item
x
at indexi
. Shifts the element currently at that position (if any) and subsequent elements to the right. - Use Case: To insert an item at a specific position in the list.
lst = [1, 2, 4, 5]
lst.insert(2, 3) # Insert 3 at index 2
print(lst) # Output: [1, 2, 3, 4, 5]
- Description: Removes and returns the item at index
i
. Ifi
is not specified,pop()
removes and returns the last item in the list. - Use Case: Useful for both removing an item from a specific position and for stack-like behavior (LIFO).
lst = [1, 2, 3, 4]
print(lst.pop()) # Output: 4 (removes and returns the last item)
print(lst.pop(0)) # Output: 1 (removes and returns the first item)
- Description: Removes the first occurrence of item
x
from the list. Raises aValueError
if the item is not found. - Use Case: To remove a specific item from a list.
lst = [1, 2, 3, 2, 4]
lst.remove(2)
print(lst) # Output: [1, 3, 2, 4] (first 2 is removed)
- Description: Reverses the elements of the list in place.
- Use Case: To reverse the order of elements in a list.
lst = [1, 2, 3, 4, 5]
lst.reverse()
print(lst) # Output: [5, 4, 3, 2, 1]
- Description: Sorts the list in place. The optional
key
argument specifies a function of one argument that is used to extract a comparison key from each list element. The optionalreverse
argument is a boolean value; if set toTrue
, the list elements are sorted as if each comparison were reversed. - Use Case: To sort elements in a list either in ascending or descending order.
lst = [3, 1, 4, 2, 5]
lst.sort()
print(lst) # Output: [1, 2, 3, 4, 5]
lst.sort(reverse=True)
print(lst) # Output: [5, 4, 3, 2, 1]
# Sorting by a key
words = ['apple', 'banana', 'cherry']
words.sort(key=len)
print(words) # Output: ['apple', 'banana', 'cherry'] (sorted by length)
You can check if two lists are the same object using the is
operator. However, even if two lists have the same elements, they are not the same object unless explicitly assigned.
a = [1, 2, "Peter", 4.50, "Ricky", 5, 6]
b = [1, 2, "Peter", 4.50, "Ricky", 5, 6]
print(a is b) # Output: False (Different objects)
print(a == b) # Output: True (Same elements)
b = a # Now b refers to the same object as a
print(a is b) # Output: True (Same object)
Lists support indexing and slicing, which allows you to access specific elements or a range of elements.
squares = [1, 4, 9, 16, 25]
print(squares[0]) # Output: 1
print(squares[-1]) # Output: 25
print(squares[-3:]) # Output: [9, 16, 25]
+---+---+---+---+---+
| 1 | 4 | 9 | 16 | 25|
+---+---+---+---+---+
0 1 2 3 4
-5 -4 -3 -2 -1
Lists can be concatenated using the +
operator, and you can modify elements by assigning new values.
squares = [1, 4, 9, 16, 25]
squares += [36, 49, 64, 81, 100] # Concatenation
print(squares) # Output: [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
cubes = [1, 8, 27, 65, 125]
cubes[3] = 64 # Correcting an incorrect value
print(cubes) # Output: [1, 8, 27, 64, 125]
Lists can contain other lists, allowing for complex data structures.
a = ['a', 'b', 'c']
n = [1, 2, 3]
x = [a, n]
print(x) # Output: [['a', 'b', 'c'], [1, 2, 3]]
print(x[0][1]) # Output: 'b'
print(x[1][2]) # Output: 3
Lists are mutable, meaning their values can be updated using slice assignments.
lst = [1, 2, 3, 4, 5, 6]
lst[2] = 10 # Update the value at index 2
print(lst) # Output: [1, 2, 10, 4, 5, 6]
lst[1:3] = [89, 78] # Update a slice of the list
print(lst) # Output: [1, 89, 78, 4, 5, 6]
lst[-1] = 25 # Update the last element
print(lst) # Output: [1, 89, 78, 4, 5, 25]
The concatenation +
and repetition *
operators work with lists just as they do with strings.
l1 = [1, 2, 3, 4]
l2 = [5, 6, 7, 8]
print(l1 * 2 + l2) # Output: [1, 2, 3, 4, 1, 2, 3, 4, 5, 6, 7, 8]
print(len(l1)) # Output: 4
You can iterate through a list using a for
loop.
lst = ["John", "David", "James", "Jonathan"]
for name in lst:
print(name) # Output: John, David, James, Jonathan (each on a new line)
You can add elements to a list using the append()
method.
l = []
n = int(input("Enter the number of elements in the list: "))
for i in range(n):
l.append(input("Enter the item: "))
print("Printing the list items...")
for item in l:
print(item, end=" ")
Enter the number of elements in the list: 5
Enter the item: 25
Enter the item: 46
Enter the item: 12
Enter the item: 75
Enter the item: 42
Printing the list items...
25 46 12 75 42
Use the remove()
method to delete an element from a list.
lst = [0, 1, 2, 3, 4]
print("Original list: ", lst)
lst.remove(2)
print("List after removal: ", lst)
Original list: [0, 1, 2, 3, 4]
List after removal: [0, 1, 3, 4]
You can modify items in a list using indexing and slicing, and you can delete items using the del
keyword.
x = [1, 2, 3, 4]
x[1] = 42 # Modify single item
print(x) # Output: [1, 42, 3, 4]
x[1:3] = [22, 33, 44] # Modify a slice
print(x) # Output: [1, 22, 33, 44, 4]
del x[1] # Delete an item
print(x) # Output: [1, 33, 44, 4]
Python’s sort()
method sorts a list in place, and it is guaranteed to be stable (elements that compare equal are not exchanged).
mylist = ['alpha', 'Beta', 'GAMMA']
mylist.sort() # Sorts alphabetically
print(mylist) # Output: ['Beta', 'GAMMA', 'alpha']
mylist.sort(key=str.lower) # Sorts case-insensitively
print(mylist) # Output: ['alpha', 'Beta', 'GAMMA']