0% found this document useful (0 votes)
0 views3 pages

Python List Methods Example

Uploaded by

hasanhiba442
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)
0 views3 pages

Python List Methods Example

Uploaded by

hasanhiba442
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/ 3

Python List Methods Example

1. append(item) - Adds an item to the end of the list

Example:

my_list.append(6)

print("After append:", my_list)

2. remove(item) - Removes the first occurrence of the specified item

Example:

my_list.remove(3)

print("After remove:", my_list)

3. pop(index) - Removes and returns the item at the specified index

Example:

popped_item = my_list.pop(2)

print("Popped item:", popped_item)

print("After pop:", my_list)

4. len(list) - Returns the number of items in the list

Example:

print("Length of the list:", len(my_list))

5. clear() - Removes all items from the list

Example:

my_list.clear()

print("After clear:", my_list)


6. insert(index, item) - Inserts an item at the specified index

Example:

my_list.insert(2, 10)

print("After insert:", my_list)

7. extend(iterable) - Adds all items from another iterable to the list

Example:

my_list.extend([6, 7, 8])

print("After extend:", my_list)

8. index(item) - Returns the index of the first occurrence of the specified item

Example:

index_of_7 = my_list.index(7)

print("Index of 7:", index_of_7)

9. count(item) - Returns the number of times the specified item appears in the list

Example:

count_of_1 = my_list.count(1)

print("Count of 1:", count_of_1)

10. sort() - Sorts the list in ascending order

Example:

my_list.sort()

print("After sort:", my_list)

11. reverse() - Reverses the elements of the list


Example:

my_list.reverse()

print("After reverse:", my_list)

12. copy() - Returns a shallow copy of the list

Example:

my_list_copy = my_list.copy()

print("List copy:", my_list_copy)

You might also like