Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Methods of List

The document provides an overview of commonly used Python list methods, including Append, Extend, Insert, Remove, Pop, Clear, Index, Count, Sort, Reverse, and Copy, each with examples. These methods allow for efficient manipulation and access of list elements. A summary table is also included for quick reference.

Uploaded by

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

Methods of List

The document provides an overview of commonly used Python list methods, including Append, Extend, Insert, Remove, Pop, Clear, Index, Count, Sort, Reverse, and Copy, each with examples. These methods allow for efficient manipulation and access of list elements. A summary table is also included for quick reference.

Uploaded by

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

In Python, lists are one of the most commonly used data structures.

They are
mutable, ordered collections of items (which can be of different types).
Python provides many built-in list methods that help in manipulating and
accessing the list elements efficiently.

Below is a list of commonly used list methods, each with an example and
explanation:

🔹 1. Append()

Adds an element at the end of the list.

Fruits = [‘apple’, ‘banana’]


Fruits.append(‘orange’)
Print(fruits)
# Output: [‘apple’, ‘banana’, ‘orange’]

🔹 2. Extend()

Adds all elements of an iterable (list, tuple, etc.) to the end of the
list.

Fruits = [‘apple’, ‘banana’]


Fruits.extend([‘grape’, ‘mango’])
Print(fruits) # Output: [‘apple’, ‘banana’, ‘grape’, ‘mango’]

🔹 3. Insert()

Inserts an element at a specified position.

Fruits = [‘apple’, ‘banana’]


Fruits.insert(1, ‘orange’)
Print(fruits)
# Output: [‘apple’, ‘orange’, ‘banana’]

🔹 4. Remove()

Removes the first occurrence of a value.

Fruits = [‘apple’, ‘banana’, ‘apple’]


Fruits.remove(‘apple’)
Print(fruits) # Output: [‘banana’, ‘apple’]

🔹 5. Pop()

Removes and returns the element at the given index (last element by
default).

Fruits = [‘apple’, ‘banana’, ‘orange’]


Item = fruits.pop()
Print(item) # Output: ‘orange’
Print(fruits) # Output: [‘apple’, ‘banana’]
🔹 6. Clear()

Removes all elements from the list.

Fruits = [‘apple’, ‘banana’]


Fruits.clear()
Print(fruits) # Output: []

🔹 7. Index()

Returns the index of the first occurrence of a value.

Fruits = [‘apple’, ‘banana’, ‘orange’]


Idx = fruits.index(‘banana’)
Print(idx) # Output: 1

🔹 8. Count()

Returns the number of times a value appears in the list.

Fruits = [‘apple’, ‘banana’, ‘apple’]


Count = fruits.count(‘apple’)
Print(count) # Output: 2

🔹 9. Sort()

Sorts the list in ascending order (modifies the list).

Numbers = [5, 2, 9, 1]
Numbers.sort()
Print(numbers) # Output: [1, 2, 5, 9]

Use numbers.sort(reverse=True) for descending order.

🔹 10. Sorted() (Not a method, but a built-in function)

Returns a new sorted list from the elements of any iterable.

Numbers = [3, 1, 4]
New_list = sorted(numbers)
Print(new_list) # Output: [1, 3, 4]

🔹 11. Reverse()

Reverses the order of elements in the list.

Numbers = [1, 2, 3]
Numbers.reverse()
Print(numbers) # Output: [3, 2, 1]
🔹 12. Copy()

Returns a shallow copy of the list.

Original = [1, 2, 3]
Copied = original.copy()
Print(copied) # Output: [1, 2, 3]

✅ Summary Table
Method Description
Append() Adds an element to the end
Extend() Adds elements from another iterable
Insert() Inserts element at specific index
Remove() Removes first matching value
Pop() Removes and returns element by index
Clear() Removes all items
Index() Returns index of first match
Count() Counts how many times a value occurs
Sort() Sorts the list
Reverse() Reverses the list
Copy() Returns a shallow copy of the list

You might also like