Python Lists
Python Lists
Python Lists
Last Updated : 28 Oct, 2024
In Python, a list is a built-in data structure that is used to store an ordered collection of items. Lists
are mutable, meaning that their contents can be changed after the list has been created. They can
hold a various of data types, including integers, floats, strings, and even other lists.
Python
print(a)
Output
Table of Content
Creating a List
Creating a List with Repeated Elements
Accessing List Elements
Modifying Lists
Adding Elements into List
Updating Elements into List
Removing Elements from List
Iterating Over Lists
Nested Lists and Multidimensional Lists
Basic Example on Python List
Creating a List
We can create a list in Python using square brackets [] or by using the list() constructor. Here are
some common methods to create a list:
https://www.geeksforgeeks.org/python-lists/?ref=lbp 1/6
11/12/24, 4:33 PM Python Lists - GeeksforGeeks
Python
# List of integers
a = [1, 2, 3, 4, 5]
# List of strings
b = ['apple', 'banana', 'cherry']
print(a)
print(b)
print(c)
Output
[1, 2, 3, 4, 5]
['apple', 'banana', 'cherry']
[1, 'hello', 3.14, True]
We can also create a list by passing an iterable (like a string, tuple, or another list) to
the list() function.
Python
# From a tuple
a = list((1, 2, 3, 'apple', 4.5))
print(a)
Output
https://www.geeksforgeeks.org/python-lists/?ref=lbp 2/6
11/12/24, 4:33 PM Python Lists - GeeksforGeeks
Python
print(a)
print(b)
Output
[2, 2, 2, 2, 2]
[0, 0, 0, 0, 0, 0, 0]
Python
Output
10
50
https://www.geeksforgeeks.org/python-lists/?ref=lbp 3/6
11/12/24, 4:33 PM Python Lists - GeeksforGeeks
Python
# Inserting 5 at index 0
a.insert(0, 5)
print("After insert(0, 5):", a)
Output
Python
print(a)
Output
https://www.geeksforgeeks.org/python-lists/?ref=lbp 4/6
11/12/24, 4:33 PM Python Lists - GeeksforGeeks
Python
Output
Python
https://www.geeksforgeeks.org/python-lists/?ref=lbp 5/6
11/12/24, 4:33 PM Python Lists - GeeksforGeeks
# Iterating over the list
for item in a:
print(item)
Output
apple
banana
cherry
Python
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
Output
https://www.geeksforgeeks.org/python-lists/?ref=lbp 6/6