Python List Functions Examples

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.

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:

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.

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).

.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.

.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.

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.