List is a built-in data structure used to store an ordered collection of items. They are dynamic, resizable and capable of storing multiple data types.
- Mutable: list elements can be changed, updated, added, or removed after the list is created.
- Ordered: elements maintain the order in which they are inserted.
- Index-based: elements are accessed using their position, starting from index 0.
Creating a List
Lists can be created in several ways, such as using square brackets [] , the list() constructor or by repeating elements.
1. Using Square Brackets: Square brackets [] are used to create a list directly.
a = [1, 2, 3]
print(a)
b = ["apple", "banana"]
print(b)
Output
[1, 2, 3] ['apple', 'banana']
2. Using list() Constructor: A list can also be created by passing an iterable (such as tuple, string or another list) to the list() constructor.
a = list((1, 2, 3, 'apple', 4.5))
print(a)
b = list("GFG")
print(b)
Output
[1, 2, 3, 'apple', 4.5] ['G', 'F', 'G']
3. Creating List with Repeated Elements: A list with repeated elements can be created using the multiplication (*) operator.
a = [2] * 5
b = [0] * 7
print(a)
print(b)
Output
[2, 2, 2, 2, 2] [0, 0, 0, 0, 0, 0, 0]
Internal Representation of Lists
Python list stores references to objects, not the actual values directly.
- The list keeps memory addresses of objects like integers, strings or booleans.
- Actual objects exist separately in memory.
- Modifying a mutable object inside a list changes the original object.
- Reassigning an immutable object creates a new object instead of changing the old one.
a = [1, 2, 2, "Python"]
print(a[0]) # index-based
print(a)
Output
1 [1, 10, 2, 'Python']
Explanation:
- The list a contains an integer (10, 20 and 40), a string ("GfG") and a boolean (True).
- Elements are accessed using indexing (a[0], a[1], etc.).
- Each element keeps its original type.

Accessing List Elements
Elements in a list are accessed using indexing. Python uses zero-based indexing, meaning a[0] represents the first element. Negative indexing is also supported, where -1 accesses the last element.
a = [10, 20, 30]
print(a[0])
print(a[-1])
Output
10 30
Adding Elements into List
Elements can be added to a list using the following methods:
1. append(): Adds an element at the end of the list.
a = [1, 2]
a.append(3)
print(a)
Output
[1, 2, 3]
2. insert(): Adds an element at a specific position.
a = [1, 3]
a.insert(1, 2)
print(a)
Output
[1, 2, 3]
3. extend(): Adds multiple elements to the end of the list.
a = [1, 2]
a.extend([3, 4])
print(a)
Output
[1, 2, 3, 4]
Updating Elements into List
Since lists are mutable, elements can be updated by assigning new values using their index.
a = [10, 20, 30, 40, 50]
a[1] = 25
print(a)
Output
[10, 25, 30, 40, 50]
Removing Elements from List
Elements can be removed from a list using the following methods:
1. remove(): Removes the first occurrence of an element.
a = [1, 2, 3]
a.remove(2)
print(a)
Output
[1, 3]
2. pop(): Removes the element at a specific index or the last element if no index is specified.
a = [1, 2, 3]
a.pop()
print(a)
Output
[1, 2]
3. del statement: Deletes an element at a specified index.
a = [1, 2, 3]
del a[1]
print(a)
4. clear(): removes all items.
a = [1, 2, 3]
a.clear()
print(a)
Output
[]
Iterating Over Lists
Lists can be iterated using loops, allowing operations to be performed on each element.
a = ['apple', 'banana', 'cherry']
for item in a:
print(item)
Output
apple banana cherry
Nested Lists
A nested list is a list that contains another list as its element. It is commonly used to represent matrices or tabular data. Nested elements can be accessed by chaining multiple indexes.
a = [[1, 2], [3, 4]]
print(a[0])
print(a[1][0])
Output
[1, 2] 3
To know more, please refer to this article: List comprehension
Recommended Problems: List Traversal, Length of The List, Sum The List, Decrement List Values, Append To List