The methods of lists

In Python, lists have eleven methods. We conditionally divide them into groups:

The append and extend methods add to the end of the list. The difference between the two is that append can only append one element, while extend allows extend the list at once on some values. Both methods take one argument. In the case of extend, it must be an iterable object (tuple, list, string, etc.), each element of which will become a separate item of the list.

>>> lst = ['a', 45, 89, 'who']
>>> lst.append(67)
>>> lst
['a', 45, 89, 'who', 67]
>>> b
[1, 2, 3]
>>> c = (9, 10)
>>> b.extend(c)
>>> b
[1, 2, 3, 9, 10]
>>> b.extend("abc")
>>> b
[1, 2, 3, 9, 10, 'a', 'b', 'c']
>>> b.extend([12, 19])
>>> b
[1, 2, 3, 9, 10, 'a', 'b', 'c', 12, 19]

If you want to insert an element at an arbitrary position in the list, use the insert method. It takes two arguments: first the index, then the value. The item is inserted before the element that previously occupied the specified position.

>>> lst.insert(0,10)
>>> lst
[10, 'a', 45, 89, 'who', 67, 'a1', (1, 2, 3)]
>>> lst.insert(len(lst),10)
>>> lst
[10, 'a', 45, 89, 'who', 67, 'a1', (1, 2, 3), 10]
>>> lst.insert(3, 10)
>>> lst
[10, 'a', 45, 10, 89, 'who', 67, 'a1', (1, 2, 3), 10]

To remove a single element from a list, use the remove and pop methods. The remove method takes the value of the element to be removed, and removes the first occurrence of it. If the item is not in the list, a ValueError exception is thrown. The pop method removes the item by index. It returns the value removed from the list to the program. Calling pop() with no arguments removes and returns the last item. The pop method throws a IndexError exception if an index is specified that is outside the list's index range.

lst = [4, 3, 5, 1, 8, 1]

d = int(input())

try:
    lst.remove(d)
except ValueError:
    print('No the item')

print(lst)
1
[4, 3, 5, 8, 1]
2
No the item
[4, 3, 5, 1, 8, 1]
lst = ['a', 'f', 'b', 'x', 'y', 'k']

i = int(input())

try:
    value = lst.pop(i)
except IndexError:
    value = 'Index Error'

print(value)
2
b
-1
k
10
Index Error

The clear method removes all items from the list.

>>> a = [1, 2]
>>> a.clear()
>>> a
[]

The reverse method reverses the order of items. The method reverses the list in place, that is, the one it is applied to.

>>> lst
['a', 10, 89, 'who', 67, 'a1', (1, 2, 3), 10]
>>> lst.reverse()
>>> lst
[10, (1, 2, 3), 'a1', 67, 'who', 89, 10, 'a']

The sort method performs a sort of the list in place (the list is modified rather than returning a new one). If sort() is called with no arguments, the sort is in ascending order. To sort in descending order, set the named parameter reverse to True.

>>> li = [4, 1, 9, 5]
>>> li.sort()
>>> li
[1, 4, 5, 9]
>>> st = [4, 2, 7, 5]
>>> st.sort(reverse=True)
>>> st
[7, 5, 4, 2]

For more complex sortings, the key parameter is used, which is assigned a function that performs actions on each item of the list. Sorting occurs based on the returned values.

>>> n = [-4, 3, 9, -5, 2]
>>> n.sort(key=lambda i: abs(i))
>>> n
[2, 3, -4, -5, 9]

The count method counts the number of times the given argument occurs in the list.

>>> a = [1, 0, 1, 1, 0]
>>> a.count(1)
3

The index method returns the index of the given item. If there are multiple such values, the method will return the index of the first one only. If there are no such items, an exception is thrown. You can specify a slice to search for as the second and third arguments.

>>> a = ['a', 'c', 'e', 'a', 'b']
>>> a.index('a')
0
>>> a.index('a', 2)
3
>>> a.index('a', 2, 4)
3
>>> a.index('a', 2, 3)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: 'a' is not in list

The copy method creates a shallow copy of the list. So if there are nested lists, only links to them are copied. As a result, changing nested lists through the original list will also be visible in the copy list.

>>> a = [1, 2]
>>> b = a.copy()
>>> b.append(3)
>>> a
[1, 2]
>>> b
[1, 2, 3]
>>> c = [1, 2, [3, 4]]
>>> d = c.copy()
>>> d.append(5)
>>> c[2].append(6)
>>> c
[1, 2, [3, 4, 6]]
>>> d
[1, 2, [3, 4, 6], 5]