Wednesday, November 9, 2022

List Functions and Examples in Python

 

Function Operation Example
list.append(x) Append x to end of the list >>> list=[1,2,3]
>>> list.append(4)
>>> print(list)
[1, 2, 3, 4]
list.count(x) Count the no of x >>> list.count(1)
1
list.index(x) Return the index of x >>> list.index(2)
1
list.insert(i,x) Insert x before index  i >>> list.insert(1,5)
>>> print(list)
[1, 5, 2, 3, 4]
1
list.pop(i) Remove and return the item at index i >>> list.pop(3)
3
list.remove(x) Remove x from the list >>> list.remove(5)
>>> print(list)
[1, 2, 4]
list.reverse() Reverse the sequence of list >>> list.reverse()
>>> print(list)
[4, 2, 1]
list.sort() Sort the elements in list >>> list.sort()
>>> print(list)
[1, 2, 4]
list.extend(x) Appends the contents of x to list >>> x=[7,8,9]
>>> list.extend(x)
>>> print(list)
[1, 2, 4, 7, 8, 9]
len(list) Return the length of list >>> print(len(list))
6
max(list) Return the max from the list >>> print(max(list))
9
min(list) Return the min from the list >>> print(min(list))
1


Related Posts


No comments :

Post a Comment