Lists in Python

Lists in Python

Now, we’ll see what methods the list data includes:

>>> r = [1,2,3,4,5]
>>> type(r)
<class ‘list’>
>>> dir(r)
[‘__add__’, ‘__class__’, ‘__contains__’, ‘__delattr__’, ‘__delitem__’, ‘__dir__’
, ‘__doc__’, ‘__eq__’, ‘__format__’, ‘__ge__’, ‘__getattribute__’, ‘__getitem__’
, ‘__gt__’, ‘__hash__’, ‘__iadd__’, ‘__imul__’, ‘__init__’, ‘__iter__’, ‘__le__’
, ‘__len__’, ‘__lt__’, ‘__mul__’, ‘__ne__’, ‘__new__’, ‘__reduce__’, ‘__reduce_e
x__’, ‘__repr__’, ‘__reversed__’, ‘__rmul__’, ‘__setattr__’, ‘__setitem__’, ‘__s
izeof__’, ‘__str__’, ‘__subclasshook__’, ‘append’, ‘clear’, ‘copy’, ‘count’, ‘ex
tend’, ‘index’, ‘insert’, ‘pop’, ‘remove’, ‘reverse’, ‘sort’]
>>>

There are some. They are useful, so we should know them.

The following list mutators update a list object and do not return a value.

>>> r = [1,2,3,4,5]

list.append(object)

>>> r.append(6)
>>> r
[1, 2, 3, 4, 5, 6]
>>> r.append(“Bolek”)
>>> r
[1, 2, 3, 4, 5, 6, ‘Bolek’]
>>>

Update list by appending object to end of the list.

list.extend(list)

>>> p = [“Tolek”, “Olek”, “Lolek”]
>>> p
[‘Tolek’, ‘Olek’, ‘Lolek’]
>>> type(p)
<class ‘list’>
>>>

>>> r.extend(p)
>>> r
[1, 2, 3, 4, 5, 6, ‘Bolek’, ‘Tolek’, ‘Olek’, ‘Lolek’]
>>>

Extend list by appending list elements. Note the difference from append(), which treats the argument as a single list object.

list.insert(index, object)

>>> r.insert(4, “Husaria”)
>>> r
[1, 2, 3, 4, ‘Husaria’, 5, 6, ‘Bolek’, ‘Tolek’, ‘Olek’, ‘Lolek’]
>>> r.insert(10, “Husaria”)
>>> r
[1, 2, 3, 4, ‘Husaria’, 5, 6, ‘Bolek’, ‘Tolek’, ‘Olek’, ‘Husaria’, ‘Lolek’]
>>> r.insert(25, “Husaria”)
>>> r
[1, 2, 3, 4, ‘Husaria’, 5, 6, ‘Bolek’, ‘Tolek’, ‘Olek’, ‘Husaria’, ‘Lolek’, ‘Hus
aria’]
>>> r.insert(-1, “Husaria”)
>>> r
[1, 2, 3, 4, ‘Husaria’, 5, 6, ‘Bolek’, ‘Tolek’, ‘Olek’, ‘Husaria’, ‘Lolek’, ‘Hus
aria’, ‘Husaria’]
>>> r.insert(-10, “Husaria”)
>>> r
[1, 2, 3, 4, ‘Husaria’, ‘Husaria’, 5, 6, ‘Bolek’, ‘Tolek’, ‘Olek’, ‘Husaria’, ‘L
olek’, ‘Husaria’, ‘Husaria’]
>>>

Update list by inserting object before position index. If index is greater than len(list), the object is simply appended. If index is less than zero, the object is prepended.

list.pop([index=-1]) → item

>>> r
[1, 2, 3, 4, ‘Husaria’, ‘Husaria’, 5, 6, ‘Bolek’, ‘Tolek’, ‘Olek’, ‘Husaria’, ‘L
olek’, ‘Husaria’, ‘Husaria’]

>>> r.pop()
‘Husaria’
>>> r
[1, 2, 3, 4, ‘Husaria’, ‘Husaria’, 5, 6, ‘Bolek’, ‘Tolek’, ‘Olek’, ‘Husaria’, ‘L
olek’, ‘Husaria’]
>>>

>>> r.pop(3)
4
>>> r
[1, 2, 3, ‘Husaria’, ‘Husaria’, 5, 6, ‘Bolek’, ‘Tolek’, ‘Olek’, ‘Husaria’, ‘Lole
k’, ‘Husaria’]
>>>

Remove and return item at index (default last, -1) in list. An exception is raised if the list is already empty. In the case of the pop() method, it both returns information as well as mutates the list.

list.remove(value)

>>> r.remove(‘Husaria’)
>>> r
[1, 2, 3, ‘Husaria’, 5, 6, ‘Bolek’, ‘Tolek’, ‘Olek’, ‘Husaria’, ‘Lolek’, ‘Husari
a’]
>>> r.remove(‘Tolek’)
>>> r
[1, 2, 3, ‘Husaria’, 5, 6, ‘Bolek’, ‘Olek’, ‘Husaria’, ‘Lolek’, ‘Husaria’]
>>>

Remove first occurrence of value from list. An exception is raised if the value is not in the list.

list.reverse()

>>> r.reverse()
>>> r
[‘Husaria’, ‘Lolek’, ‘Husaria’, ‘Olek’, ‘Bolek’, 6, 5, ‘Husaria’, 3, 2, 1]
>>>

Reverse the items of the list. This is done “in place”, it does not create a new list. There is no return value.

list.sort([key][, reverse=False])

>>> t = [1,2,3,4,5,6]
>>> u = [“Tolek”, “Bolek”, “Lolek”, “Molek”, “Wolek”]

>>> t.sort()
>>> t
[1, 2, 3, 4, 5, 6]
>>> u.sort()
>>> u
[‘Bolek’, ‘Lolek’, ‘Molek’, ‘Tolek’, ‘Wolek’]

>>> t.sort(reverse=True)
>>> t
[6, 5, 4, 3, 2, 1]
>>> u.sort(reverse=True)
>>> u
[‘Wolek’, ‘Tolek’, ‘Molek’, ‘Lolek’, ‘Bolek’]
>>>

>>> t.sort(reverse=False)
>>> t
[1, 2, 3, 4, 5, 6]
>>> u.sort(reverse=False)
>>> u
[‘Bolek’, ‘Lolek’, ‘Molek’, ‘Tolek’, ‘Wolek’]
>>>

Sort the items of the list. This is done “in place”, it does not create a new list.

If the reverse keyword parameter is provided and set to True, the tuple is sorted into descending order.

The key parameter is used when the items in the tuple aren’t simply sorted using the default comparison operators. The key function must return the fields to be compared selected from the underlying items in the tuple.

The following accessor methods provide information about a list.

list.count(value) → integer

>>> t.count(3)
1
>>> u.count(“Tolek”)
1
>>>

Return number of occurrences of value in list.

list.index(value) → integer

>>> t.index(3)
2
>>> u.index(“Tolek”)
3
>>>

Return index of first occurrence of value in list.

Leave a comment