0% found this document useful (0 votes)
20 views5 pages

Practical 2 Aadi

Uploaded by

s2564m47y
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)
20 views5 pages

Practical 2 Aadi

Uploaded by

s2564m47y
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

PEN:220840131132

PRACTICAL :- 2
Understand and use different methods of List.
Theory of Lists:
• Lists are fundamental data structures in Python.
• They are ordered, mutable collections that can store elements of various data types
(integers, strings, booleans, etc.) within square brackets [].
• Elements are accessed and manipulated using zero-based indexing, where the first
• element has an index of 0, the second has an index of 1, and so on..

Understanding and Using List Methods


Here's a breakdown of essential list methods in Python:

1. Declaration:
In Python, you can declare a list using square brackets []. An empty list can be created as well.
Here's an example:
Code:
my_list = [1,2,3,4]
print(my_list)
Output:

2. Append:
The append() method adds an element to the end of the list. It modifies the original list.

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

Output:

Page No.7
PEN:220840131132

3.Extend:
The extend() method adds all the elements from an iterable (like another list) to the end of the
current list. It also modifies the original list.
Code:
my_list = [1,2,3,4]
my_list.extend(5,6)
print(my_list)
Output:

4.Index:
The index() method returns the index (position) of the first occurrence of a specified value in
the list.
Code:
my_list = [1,2,3,4]
my_list.index(3)
Output:

5.Count:
The count() method returns the number of elements with a specified value in the list.
Code:
my_list = [1,2,3,4]
my_list.count(3)
Output:

Page No.8
PEN:220840131132

6.Copy
The copy() method returns a shallow copy of the list. A new list is created with the same
elements as the original list. Changes to the copy won't affect the original and vice versa Python
Code:
my_list = [1,2,3,4]
my_list1 = my_list.copy()
my_list1.append(5)
print(my_list)
print(my_list1)
Output:

7.Clear:
The clear() method removes all elements from the list.
Code:
my_list = [1, 2, 3, 4]
my_list.clear()
print(my_list)
Output:

8.Pop:
The pop() method removes and returns the element at a specified index (position) in the list.
By default, it removes the last element (index -1).
Code:
my_list = [1, 2, 3, 4]
removed_element = my_list.pop()
print(my_list)
print(removed_element)

Page No.9
PEN:220840131132

Output:

9.Insert:
The insert() method inserts an element at a specified index in the list. It modifies the original
list.
Code:
my_list = [1,2,3,4]
my_list.insert(2,8)
print(my_list)
Output:R

10.Remove:
The remove() method removes the first occurrence of a specified value from the list. It
modifies the original list
Code:
my_list = [1,2,3,4]
my_list.remove(2)
print(my_list)
Output:

11.Reverse:
The reverse() method reverses the order of elements in the list. It modifies the original list.
Code:
my_list = [1,2,3,4]
my_list.reverse()
print(my_list)

Page No.10
PEN:220840131132

Output:

12. Sort:
The sort() method sorts the elements of the list in ascending order by default. It modifies the
original list. You can use the reverse argument to sort in descending order.
Code:
my_list = [3,2,1,4]
my.list.sort()
print(my_list)

Output:

Page No.11

You might also like