0% found this document useful (0 votes)
4 views2 pages

List Functions

Uploaded by

johnslaiju7b
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)
4 views2 pages

List Functions

Uploaded by

johnslaiju7b
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

S.

No Method Description Example

1 append(x) Adds an item x to the end of the list. lst.append(10)

2 extend(iterable) Extends the list by appending lst.extend([3, 4])


elements from another iterable.

3 insert(i, x) Inserts item x at position i. lst.insert(1,


"hello")

4 remove(x) Removes the first occurrence of lst.remove(3)


value x.

5 pop(i) Removes and returns the item at lst.pop() or


index i. Default: last item. lst.pop(2)

6 clear() Removes all items from the list. lst.clear()

7 index(x) Returns the index of the first lst.index("apple")


occurrence of value x.

8 count(x) Returns the number of times x lst.count(5)


appears in the list.

9 sort() Sorts the list in ascending order (in- lst.sort()


place).

10 reverse() Reverses the list in-place. lst.reverse()

11 copy() Returns a shallow copy of the list. copy_lst =


lst.copy()
S.N Function/Method Description Example
o
1 list.sort() Sorts the list in-place. numbers.sort()
Modifies the original
list.

2 sorted(list) Returns a new sorted list new_list = sorted(numbers)


without modifying the
original.

3 sort(reverse=True) Sorts in descending numbers.sort(reverse=True)


order.

4 sorted(list, Custom sorting using a sorted(names,


key=...) key=str.lower)
key function.

5 list.sortkey=...) In-place custom sorting students.sort(key=lambda x:


x[1])
using a key function.

You might also like