0% found this document useful (0 votes)
3 views

Lists in Python

Python lists are dynamic arrays that can store mixed types of items, including duplicates, and are mutable, allowing modifications. Lists can be created using square brackets or the list() constructor, and elements can be accessed, added, updated, or removed using various methods. Nested lists can also be created to represent matrices, with elements accessed through chained indexing.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lists in Python

Python lists are dynamic arrays that can store mixed types of items, including duplicates, and are mutable, allowing modifications. Lists can be created using square brackets or the list() constructor, and elements can be accessed, added, updated, or removed using various methods. Nested lists can also be created to represent matrices, with elements accessed through chained indexing.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Python Lists

In Python, a list is a built-in dynamic sized array (automatically grows and shrinks). We can store all
types of items (including another list) in a list. A list may contain mixed type of items, this is possible
because a list mainly stores references at contiguous locations and actual items may be stored at
different locations. List can contain duplicate items. List in Python are Mutable. Hence, we can
modify, replace or delete the items. Lists are ordered. It maintains the order of elements based on
how they are added. Accessing items in List can be done directly using their position (index), starting
from 0.

# example

a = [10, 20, 15]

print(a[0]) # access first item

a.append(11) # add item

a.remove(20) # remove item

print(a)

Creating a List

Using Square Brackets


# 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)

Using list() Constructor

We can also create a list by passing an iterable (like a string, tuple, or another list) to list() function.

# From a tuple
a = list((1, 2, 3, 'apple', 4.5))

print(a)

Creating List with Repeated Elements

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

# 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)

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.

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

# Access first element

print(a[0]) # ouput is 10

# Access last element

print(a[-1]) # output is 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.


 extend(): Adds multiple elements to the end of the list.
 insert(): Adds an element at a specific position.

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

# Output -After insert(0, 5): [5, 10]

# Output -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.

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

# Change the second element

a[1] = 25

print(a)

# output - [10, 25, 30, 40, 50]

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.

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)

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

# ouput - Popped element: 20

# ouput - After pop(1): [10, 40, 50]

# ouput - 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.

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

# Iterating over the list

for item in a:

print(item)

Output

apple

banana

cherry

Nested Lists in Python

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.

matrix = [

[1, 2, 3],

[4, 5, 6],

[7, 8, 9]

# Access element at row 2, column 3


print(matrix[1][2])

Output

You might also like