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

LISTS - UNIT 2

The document provides an overview of lists in Python, covering their definition, slicing, methods, and mutability. It explains how to manipulate lists using built-in methods, the concept of aliasing, cloning techniques, and how lists can be passed as parameters to functions. Examples are included to illustrate each concept clearly.

Uploaded by

Dept CSCA
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 views

LISTS - UNIT 2

The document provides an overview of lists in Python, covering their definition, slicing, methods, and mutability. It explains how to manipulate lists using built-in methods, the concept of aliasing, cloning techniques, and how lists can be passed as parameters to functions. Examples are included to illustrate each concept clearly.

Uploaded by

Dept CSCA
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/ 5

V SURESH KUMAR HOD-COMPUTER APPPLICATIONS

LISTS: List-list slices - list methods - list loop–mutability–aliasing - cloning


lists - list parameters.
List

List is used to store ordered collection of items

Example

A=[10,10.5,’a’]

print(a)

Output

[10,10.5,’a’]

Example

type(a)

Output

<class ‘list’>

List is enclosed with in square brackets []. We can change the


data in the list by using index

Example

A[0]=30

Print(a)

Output

[30,10.5,10]

1. List Slicing in Python

List slicing allows you to extract a part of a list by


specifying a range of indices.

It’s done using the syntax list[start:end:step]:

my_list = [0, 1, 2, 3, 4, 5, 6]

# Basic slicing
print(my_list[1:4])

Output: [1, 2, 3] (elements at indices 1, 2, 3)


V SURESH KUMAR HOD-COMPUTER APPPLICATIONS

# Slicing with step


print(my_list[::2])

# Output: [0, 2, 4, 6] (every second element)

# Slicing from start to index 3


print(my_list[:4])

# Output: [0, 1, 2, 3]

# Slicing from index 3 to end


print(my_list[3:])

# Output: [3, 4, 5, 6]

# Negative indices (counting from the end)


print(my_list[-3:])

# Output: [4, 5, 6]

2. List Methods in Python

Python lists come with a variety of built-in methods that


allow you to manipulate them.

Here are some commonly used list methods:

• append(x): Adds item x to the end of the list.

my_list = [1, 2, 3]
my_list.append(4)
print(my_list)

# Output: [1, 2, 3, 4]

• extend(iterable): Extends the list by appending elements


from an iterable (like another list).

my_list = [1, 2]
my_list.extend([3, 4])
print(my_list)

# Output: [1, 2, 3, 4]

• insert(i, x): Inserts item x at index i.

my_list = [1, 2, 3]
my_list.insert(1, 'a')
print(my_list) # Output: [1, 'a', 2, 3]

• remove(x): Removes the first occurrence of item x.


V SURESH KUMAR HOD-COMPUTER APPPLICATIONS
my_list = [1, 2, 3, 2]
my_list.remove(2)
print(my_list) # Output: [1, 3, 2]

• pop([i]): Removes and returns the item at index i. If i is


not provided, it removes and returns the last item.

my_list = [1, 2, 3]
item = my_list.pop()
print(item) # Output: 3
print(my_list) # Output: [1, 2]

• index(x): Returns the index of the first occurrence of x.

my_list = [1, 2, 3]
print(my_list.index(2)) # Output: 1

• count(x): Returns the number of occurrences of x in the


list.

my_list = [1, 2, 2, 3]
print(my_list.count(2)) # Output: 2

• sort(): Sorts the list in-place.

my_list = [3, 1, 2]
my_list.sort()
print(my_list) # Output: [1, 2, 3]

• reverse(): Reverses the list in-place.

my_list = [1, 2, 3]
my_list.reverse()
print(my_list) # Output: [3, 2, 1]

3. List Looping

You can loop through a list in Python using a for loop:

my_list = [1, 2, 3, 4]
for item in my_list:
print(item)

Output:

1
2
3
4
V SURESH KUMAR HOD-COMPUTER APPPLICATIONS
4. Mutability

Lists in Python are mutable, meaning you can change their


content after they are created. You can modify, add, or remove
elements from a list.

Example of mutability:

my_list = [1, 2, 3]
my_list[0] = 10 # Modify an element
print(my_list) # Output: [10, 2, 3]

my_list.append(4) # Add an element


print(my_list) # Output: [10, 2, 3, 4]

5. Aliasing

When you assign one list to another variable, the two


variables point to the same list in memory. This is called
aliasing. Any changes made to one list will also affect the
other because they refer to the same object.

list_a = [1, 2, 3]
list_b = list_a # list_b is now an alias of list_a

list_b.append(4)
print(list_a) # Output: [1, 2, 3, 4]
print(list_b) # Output: [1, 2, 3, 4]

Both list_a and list_b are now referring to the same list object,
so changes to one are reflected in the other.

6. Cloning Lists

To clone (create a copy of) a list, you can use several methods:

• Using the copy() method:

original_list = [1, 2, 3]
cloned_list = original_list.copy()
cloned_list.append(4)
print(original_list) # Output: [1, 2, 3]
print(cloned_list) # Output: [1, 2, 3, 4]

• Using slicing:

original_list = [1, 2, 3]
cloned_list = original_list[:]
cloned_list.append(4)
print(original_list) # Output: [1, 2, 3]
print(cloned_list) # Output: [1, 2, 3, 4]

• Using the list() constructor:


V SURESH KUMAR HOD-COMPUTER APPPLICATIONS
original_list = [1, 2, 3]
cloned_list = list(original_list)
cloned_list.append(4)
print(original_list) # Output: [1, 2, 3]
print(cloned_list) # Output: [1, 2, 3, 4]

7. List Parameters

Lists can be passed as parameters to functions. They are


passed by reference, so if you modify the list inside the
function, the changes will be reflected outside the function as
well.

def modify_list(my_list):
my_list.append(4) # Modifies the original list
my_list[0] = 100 # Changes the first element
original_list = [1, 2, 3]
print(original_list)
modify_list(original_list)
print(original_list)

Output:
[1, 2, 3]
[100, 2, 3, 4]

You might also like