0% found this document useful (0 votes)
2 views6 pages

Python Lists

The document provides an overview of Python lists, a mutable data structure used to store ordered collections of items. It covers various aspects such as creating lists, accessing and modifying elements, adding and removing items, and iterating over lists, including examples for each operation. Additionally, it explains nested lists and their usage for representing multidimensional data.

Uploaded by

vinku0419
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views6 pages

Python Lists

The document provides an overview of Python lists, a mutable data structure used to store ordered collections of items. It covers various aspects such as creating lists, accessing and modifying elements, adding and removing items, and iterating over lists, including examples for each operation. Additionally, it explains nested lists and their usage for representing multidimensional data.

Uploaded by

vinku0419
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

11/12/24, 4:33 PM Python Lists - GeeksforGeeks

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.

Let’s take a quick example for Python list:

Python

a = [1, 'apple', 3.14, [5, 6]]

print(a)

Output

[1, 'apple', 3.14, [5, 6]]

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:

Using Square Brackets

We can directly create a list by enclosing elements in square brackets.

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']

# Mixed data types


c = [1, 'hello', 3.14, True]

print(a)
print(b)
print(c)

Output

[1, 2, 3, 4, 5]
['apple', 'banana', 'cherry']
[1, 'hello', 3.14, True]

Using the list() Constructor

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

[1, 2, 3, 'apple', 4.5]

Creating a List with Repeated Elements


We can create a list with repeated elements using the multiplication operator.

https://www.geeksforgeeks.org/python-lists/?ref=lbp 2/6
11/12/24, 4:33 PM Python Lists - GeeksforGeeks

Python

# Create a list [2, 2, 2, 2, 2]


a = [2] * 5

# Create a list [0, 0, 0, 0, 0, 0, 0]


b = [0] * 7

print(a)
print(b)

Output

[2, 2, 2, 2, 2]
[0, 0, 0, 0, 0, 0, 0]

Accessing List Elements


Elements in a list can be accessed using indexing. Python indexes start at 0, so a[0] will access the
first element, while negative indexing allows us to access elements from the end of the list. Like
index -1 represents the last elements of list.

Python

a = [10, 20, 30, 40, 50]

# Access first element


print(a[0])

# Access last element


print(a[-1])

Output

10
50

Adding Elements into List


We can add elements to a list using the following methods:

append(): Adds an element at the end of the list.

https://www.geeksforgeeks.org/python-lists/?ref=lbp 3/6
11/12/24, 4:33 PM Python Lists - GeeksforGeeks

extend(): Adds multiple elements to the end of the list.


insert(): Adds an element at a specific position.

Python

# Initialize an empty list


a = []

# Adding 10 to end of list


a.append(10)
print("After append(10):", a)

# Inserting 5 at index 0
a.insert(0, 5)
print("After insert(0, 5):", a)

# Adding multiple elements [15, 20, 25] at the end


a.extend([15, 20, 25])
print("After extend([15, 20, 25]):", a)

Output

After append(10): [10]


After insert(0, 5): [5, 10]
After extend([15, 20, 25]): [5, 10, 15, 20, 25]

Updating Elements into List


We can change the value of an element by accessing it using its index.

Python

a = [10, 20, 30, 40, 50]

# Change the second element


a[1] = 25

print(a)

Output

[10, 25, 30, 40, 50]

https://www.geeksforgeeks.org/python-lists/?ref=lbp 4/6
11/12/24, 4:33 PM Python Lists - GeeksforGeeks

Removing Elements from List


We can remove elements from a list using:

remove(): Removes the first occurrence of an element.


pop(): Removes the element at a specific index or the last element if no index is specified.
del statement: Deletes an element at a specified index.

Python

a = [10, 20, 30, 40, 50]

# Removes the first occurrence of 30


a.remove(30)
print("After remove(30):", a)

# Removes the element at index 1 (20)


popped_val = a.pop(1)
print("Popped element:", popped_val)
print("After pop(1):", a)

# Deletes the first element (10)


del a[0]
print("After del a[0]:", a)

Output

After remove(30): [10, 20, 40, 50]


Popped element: 20
After pop(1): [10, 40, 50]
After del a[0]: [40, 50]

Iterating Over Lists


We can iterate the Lists easily by using a for loop or other iteration methods. Iterating over lists is
useful when we want to do some operation on each item or access specific items based on certain
conditions. Let’s take an example to iterate over the list using for loop.

Using for Loop

Python

a = ['apple', 'banana', 'cherry']

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

To learn various other methods, please refer to iterating over lists.

Nested Lists and Multidimensional Lists


A nested list is a list within another list, which is useful for representing matrices or tables. We can
access nested elements by chaining indexes.

Python

matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]

# Access element at row 2, column 3


print(matrix[1][2])

Output

To learn more, please refer to Multi-dimensional lists in Python

https://www.geeksforgeeks.org/python-lists/?ref=lbp 6/6

You might also like