List Methods in Python .

 List Methods in Python

The list data type has methods which allow operations on the list objects.

Build in Python List Methods





list.append(x)
Add an item to the end of the list. Equivalent to a[len(a):] = [x].e.
li = ['cow', 'chicken']
li.append('goat')
print(li)

#output:
['cow', 'chicken', 'goat']

list.clear()
Remove all items from the list. Equivalent to del a[:]. 
li = ['cow', 'chicken', 'goat']
li.clear()
print(li)

#output:
[]
 

list.copy()
Return a shallow copy of the list. Equivalent to a[:]. 
li = ['cow', 'chicken', 'goat']
li2= li.copy()
print(li2)

#output:
['cow', 'chicken', 'goat']
 

list.count(x)
Return the number of times x appears in the list. 
li = ['cow', 'chicken', 'goat', 'chicken']
c = li.count('chicken')
print(c)

#output:
2
 

list.extend(iterable)
Extend the list by appending all the items from the iterable. Equivalent to a[len(a):] = iterable. 
li = ['cow', 'chicken', 'goat', 'chicken']
li2= ['pig', 'horse']
li2.extend(li)
print(li2)

#output:
['pig', 'horse', 'cow', 'chicken', 'goat', 'chicken']
 

list.index(x[, start[, end]])
Return zero-based index in the list of the first item whose value is equal to x. Raises a ValueError if there is no such item. The optional arguments start and end are interpreted as in the slice notation and are used to limit the search to a particular subsequence of the list. The returned index is computed relative to the beginning of the full sequence rather than the start argument. 
li = ['cow', 'chicken', 'goat', 'chicken']
ind = li.index('goat')

print(ind)

#output:
2
 

list.insert(i, x)
Insert an item at a given position. The first argument is the index of the element before which to insert, so a.insert(0, x) inserts at the front of the list, and a.insert(len(a), x) is equivalent to a.append(x). 

li = ['cow', 'chicken', 'goat', 'chicken']
li.insert(1,'horse')

print(li)

#output:
['cow', 'horse', 'chicken', 'goat', 'chicken']
 

list.pop(i)
Remove the item at the given position in the list, and return it. If no index is specified, a.pop() removes and returns the last item in the list. (The square brackets around the i in the method signature denote that the parameter is optional, not that you should type square brackets at that position. You will see this notation frequently in the Python Library Reference.) 
li = ['cow', 'chicken', 'goat', 'chicken']
li.pop(1)

print(li)

#output:
['cow', 'goat', 'chicken']
 

list.remove(x)
Remove the first item from the list whose value is equal to x. It raises a ValueError if there is no such item.  
li = ['cow', 'chicken', 'goat', 'chicken']
li.remove('chicken')

print(li)

#output:
['cow', 'goat', 'chicken']

list.reverse()
Reverse the elements of the list in place. 
li = ['cow', 'chicken', 'goat', 'chicken']
li.reverse()

print(li)

#output:
['chicken', 'goat', 'chicken', 'cow']

list.sort(*, key=None, reverse=False)
Sort the items of the list in place (the arguments can be used for sort customization, see sorted() for their explanation).  
li = ['cow', 'chicken', 'goat', 'chicken']
li.sort()

print(li)

#output:
['chicken', 'chicken', 'cow', 'goat']

Comments