Table of Contents
ToggleA list in Python is a built-in data type that represents an ordered collection of items. Lists are versatile and can contain items of different data types. This guide covers everything you need to know about Python lists, including accessing, updating, deleting, and manipulating list elements.
A Python list is a sequence of comma-separated items, enclosed in square brackets [ ]. Lists can contain items of different data types, such as integers, strings, floats, and even other lists.
list1 = ["Rohan", "Physics", 21, 69.75]
list2 = [1, 2, 3, 4, 5]
list3 = ["a", "b", "c", "d"]
list4 = [25.50, True, -55, 1+2j]
Lists are ordered, meaning each item has a unique index starting from 0. They are also mutable, so you can modify their contents after creation.
You can access list elements using their index. Python also supports slicing to access a range of elements.
list1 = ['physics', 'chemistry', 1997, 2000]
list2 = [1, 2, 3, 4, 5, 6, 7]
print("list1[0]:", list1[0]) # Output: physics
print("list2[1:5]:", list2[1:5]) # Output: [2, 3, 4, 5]
You can update list elements by assigning new values to specific indices or using the append() method to add new elements.
list = ['physics', 'chemistry', 1997, 2000]
print("Value at index 2:", list[2]) # Output: 1997
list[2] = 2001
print("New value at index 2:", list[2]) # Output: 2001
You can remove elements from a list using the del statement or the remove() method.
list1 = ['physics', 'chemistry', 1997, 2000]
print("Original list:", list1) # Output: ['physics', 'chemistry', 1997, 2000]
del list1[2]
print("After deleting value at index 2:", list1) # Output: ['physics', 'chemistry', 2000]
Python lists support various operations, such as concatenation, repetition, and membership checks.
| Python Expression | Results | Description |
|---|---|---|
[1, 2, 3] + [4, 5, 6] |
[1, 2, 3, 4, 5, 6] |
Concatenation |
['Hi!'] * 4 |
['Hi!', 'Hi!', 'Hi!', 'Hi!'] |
Repetition |
3 in [1, 2, 3] |
True |
Membership |
Lists support indexing and slicing, similar to strings. You can access elements using positive or negative indices.
L = ['spam', 'Spam', 'SPAM!']
print("L[2]:", L[2]) # Output: SPAM!
print("L[-2]:", L[-2]) # Output: Spam
print("L[1:]:", L[1:]) # Output: ['Spam', 'SPAM!']
Python provides several built-in methods to manipulate lists.
| Sr.No. | Method | Description |
|---|---|---|
| 1 | list.append(obj) |
Appends an object to the list. |
| 2 | list.clear() |
Clears the contents of the list. |
| 3 | list.copy() |
Returns a shallow copy of the list. |
| 4 | list.count(obj) |
Returns the count of how many times an object occurs in the list. |
| 5 | list.extend(seq) |
Appends the contents of a sequence to the list. |
| 6 | list.index(obj) |
Returns the lowest index where the object appears. |
| 7 | list.insert(index, obj) |
Inserts an object at a specified index. |
| 8 | list.pop([index]) |
Removes and returns the object at the specified index (default is the last item). |
| 9 | list.remove(obj) |
Removes the first occurrence of the object from the list. |
| 10 | list.reverse() |
Reverses the elements of the list in place. |
| 11 | list.sort([func]) |
Sorts the elements of the list (optionally using a comparison function). |
Python provides several built-in functions to work with lists.
| Sr.No. | Function | Description |
|---|---|---|
| 1 | len(list) |
Returns the length of the list. |
| 2 | max(list) |
Returns the item with the maximum value in the list. |
| 3 | min(list) |
Returns the item with the minimum value in the list. |
| 4 | list(seq) |
Converts a sequence (e.g., tuple) into a list. |
Enroll in our Python certification course at Vista Academy and become a certified expert to boost your career.
Enroll Now