Other list operations¶
Here are some of the other list operations we frequently use in Python.* count
– number of time an element is present in a list.
sort
– to sort the data with in the list. Data in the list will be sorted in-place.
In [2]:
s ='asdfasfsafljojlsdfaljfasf'
In [3]:
l = list(s)
In [4]:
l.count?
Signature: l.count(value, /) Docstring: Return number of occurrences of value. Type: builtin_function_or_method
In [5]:
l.count('a')
Out[5]:
5
In [6]:
l.count('z')
Out[6]:
0
In [7]:
l.sort?
Signature: l.sort(*, key=None, reverse=False) Docstring: Sort the list in ascending order and return None. The sort is in-place (i.e. the list itself is modified) and stable (i.e. the order of two equal elements is maintained). If a key function is given, apply it once to each list item and sort them, ascending or descending, according to their function values. The reverse flag can be set to sort in descending order. Type: builtin_function_or_method
In [8]:
l.sort()
In [9]:
l
Out[9]:
['a', 'a', 'a', 'a', 'a', 'd', 'd', 'f', 'f', 'f', 'f', 'f', 'f', 'j', 'j', 'j', 'l', 'l', 'l', 'o', 's', 's', 's', 's', 's']
In [10]:
l.reverse?
Signature: l.reverse() Docstring: Reverse *IN PLACE*. Type: builtin_function_or_method
In [11]:
l.reverse()
In [12]:
l
Out[12]:
['s', 's', 's', 's', 's', 'o', 'l', 'l', 'l', 'j', 'j', 'j', 'f', 'f', 'f', 'f', 'f', 'f', 'd', 'd', 'a', 'a', 'a', 'a', 'a']