Python List Functions Examples

python

Python does not provide a native Array type. Instead, we have the List object. A list is simply a collection of items that are accessible by an index number, similar to arrays. Unlike Arrays, Lists can contain any value like strings, integers, floating point numbers or other objects like dictionaries or even other lists. You can also mix types of
data within a list. Lists are dynamic, so may be modified at any time. To create a list by hand, you would use the square bracket characters “[“ and “]”. Each item in a list is separated by a comma.

MyList = ['This','is','a','list']
NumberList = [0,1,2,3,4,5,6]
MyEmptyList = []
SillyList = [3,'A String',42,'42',5,'The End']

To access a single item within a list, you would do it by accessing a list by its index value. Lists have zero-based indexes, so the first item in a list is index 0, the second item is index 1, and so on. Using the above example MyList:

>>> print MyList[2]
a
>>> print MyList[3]
list
>>> print MyList[0]
This

If you attempt to access the index of a list that does not exist (index position 4 in the MyList list for example), you will get an error.
To walk (or iterate) through the entire list from beginning to end, you can use a simple for loop:

for i in range(0,len(MyList)):
print MyList[i]
This
is
a
list

An alternative way to do this is to something like the following code, which some programmers find simpler and more “pythonic” and at the same time produces the same output:
for elem in MyList:

print elem

You can also convert other types of data structures to a list. In the example below, the variable t is a tuple.

>>> t = (1,2,3)
>>> l = list(t)
>>> l
[1, 2, 3]

 

List Functions

The following built-in operators are available to the List object.

len(L)
Returns the number of items in a list.

>>> l = [1,2,3,4,5,6,7]
>>> len(l)
7

min(L)
Returns the minimum value in a list.

>>> l = [1,2,3,4,5,6,7]
>>> min(l)
1

max(L) function
Returns the maximum value in a list.

>>> l = [1,2,3,4,5,6,7]
>>> max(l)
7

x in L
Returns True if x is in the list L.

>>> l = [1,2,3,4,5,6,7]
>>> 42 in l
False
>>> 3 in l
True

x not in L
Returns True if x is NOT in the list L.

>>> l = [1,2,3,4,5,6,7]
>>> 42 not in l
True

L1 + L2
Concatenate L2 to the end of L1.

>>> l = [1,2,3,4,5,6,7]
>>> l2 = [9,10,11,12]
>>> l+l2
[1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12]

L[x]
Retrieve item in list at index position x (zero-based). This is pretty much the same thing as using an array in another language. If you need to have something that acts like a multidimensional array, which is not available in Python, you can use a list of lists.

>>> l = [1,2,3,4,5,6,7]
>>>l[3]
4

L[x1:x2]
Slice of list L from index position x1 to x2 (zero-based).

>>> l = [1,2,3,4,5,6,7]
>>> l[2:4]
[3, 4]

 

del(L[x])
Removes the item from list L at index position x (zero-based).

>>> l = ['F', 'E', 'D', 'C', 'B', 'A']
>>> del(l[2])
>>> l
['F', 'E', 'C', 'B', 'A']

 

List Methods

The following methods are available to lists.

.append(x)
Append the value in x to a list.

>>> l = [0,1,2,3,4]
>>> l.append(5)
>>> l
[0, 1, 2, 3, 4, 5]

 

.extend(L)
Append a list to another list. In the following example, l is modified, l2 is not.

>>> l = [0,1,2,3,4]
>>> l2 = [5,6,7]
>>> l.extend(l2)
>>> l
[0, 1, 2, 3, 4, 5, 6, 7]

 

.insert(i,x)
Insert a value x into list at index I. The following example inserts the value 5 at position 2
in the list.

>>> l = [0,1,2,3,4]
>>> l.insert(2,5)
>>> l
[0, 1, 5, 2, 3, 4]

If carefully used, lists with the .insert() and .pop() methods can be a quick and easy way to implement a LIFO (Last in, First Out) queue or stack.

 

.remove(x)
Removes the first item in the list that matches ‘x’. An error occurs if the item does not
exist. The following example removes the value 2 from the list. The second example tries
to do it again but gets an error.

>>> l = [0,1,2,3,4,5]
>>> l.remove(2)
>>> l
[0, 1, 3, 4, 5]
>>> l.remove(2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: list.remove(x): x not in list

 

.pop([i])
Returns and removes the last item in the list if the optional index number is not included.
If it is, it removes the item at that index (zero-based). The following example uses pop() to
remove the last item in the list, then removes the item at index position 2.

>>> l = [0,1,2,3,4,5]
>>> l.pop()
5
>>> l.pop()
4
>>> l.pop(2)
2
>>> l
[0, 1, 3]

If carefully used, lists with the .insert() and .pop() methods can be a quick and easy
way to implement a LIFO (Last in, First Out) queue or stack.

 

.index(x)
Returns the position of the item in the list.
The following example first shows the index position of the value 3 in the example list (which is 3). The second example shows the index position of the item “Oranges” in the list.

>>> l = [0,1,2,3,4,5]
>>> l.index(3)
3
>>> l1 = ['Apples','Oranges','Kiwi','Peach']
>>> l1

['Apples', 'Oranges', 'Kiwi', 'Peach']
>>> l1.index('Oranges')
1

 

.count(x)
Returns the count of the matching items in the list. If item is not in the list, it returns 0.

>>> l = [3,1,3,4,3,6,7,8]
>>> l.count(3)
3
>>> l.count(2)
0

 

.sort( )
Sorts the list from low to high.

>>> l2 = [0,1,2,3,2,5,7,3,1,2,5]
>>> l2.sort()
>>> l2
[0, 1, 1, 2, 2, 2, 3, 3, 5, 5, 7]

 

.reverse( )
Reverses the list.

>>> l = [0,1,2,3,4,5,6,7,8]
>>> l.reverse()
>>> l
[8, 7, 6, 5, 4, 3, 2, 1, 0]
>>> l = ['A','B','C','D','E','F']
>>> l.reverse()
>>> l
['F', 'E', 'D', 'C', 'B', 'A']

 

In case of any ©Copyright or missing credits issue please check CopyRights page for faster resolutions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.