Python Tutorial Series – Lists

Python provides support for several types of collections. A collection holds a group of items. An item may be a number, string, object or another collection.

One Python collection type is a list. Each item in the list may be a different data type.

Lists

Lists are used to store comma separated values enclosed in brackets, example:

to_do_list = ['groceries', 'laundry', 'wash car', 'vacuum']

Lists retain their order and elements in a list may be changed.
List elements have a corresponding index value. Indexing starts at 0 and increases by 1. Elements are accessed by using their index value.
For example this would print the third element value in the list, which is ‘wash car’:

print(to_do_list[2])

Lists may also be indexed with negative values starting at the last element. Reverse indexing starts at – 1 and decreases by -1. For example, this would also print the third element value in the list, which is ‘wash car’:

print(to_do_list[-2])

Lists may be looped through by using the in keyword:

for item in to_do_list:

    print(item)

Python provides support for many built in list functions. These functions are as follows:

append – Adds an element to the end of a list. Example:

to_do_list.append('exercise')

clear – Removes all elements from the list. Example:

to_do_list.clear()

copy – Returns a copy of the list. Example:

new_list = to_do_list.copy()

count – Returns the number of elements in the list with the given input value. Example:

to_do_list.count('vacuum')

extend – Adds to the end of a list, another list, set, tuple, etc. Example:

to_do_list.extend(['wash dishes', 'take nap'])

index – Returns the index of the input value. Example:

to_do_list.index('vacuum')

insert – Adds an element to the list in the given index position. Example:

to_do_list.insert(1, 'shovel snow')

which becomes:
to_do_list = [‘groceries’, ‘shovel snow’, ‘laundry’, ‘wash car’, ‘vacuum’]

pop – Removes an item from the list with the input index. Example:

to_do_list.pop(2)

remove – Removes an item from the list with the input value. Example:

to_do_list.remove('vacuum')

reverse – Returns the list in reverse. Example:

to_do_list.reverse()

sort – Sorts a list alphabetically. Example:

to_do_list.sort()

Sorts a list descending. Example:

to_do_list.sort(false)

A custom sort function may also be defined. Example:

 

def custom(element):
    return element[0]

to_do_list.sort(key=custom)

Returns: [‘groceries’, ‘laundry’, ‘shovel snow’, ‘vacuum’, ‘wash car’]

A couple of other useful Python methods for lists are:

del – Delete an element from a list. This does not return a value. Example:

del to_do_list[2]

    This deletes the entire list:

del to_do_list

len – Returns the number of elements in a list. Example:

num = len(to_do_list)

list – Constructor used to create a list. Example:

new_list = list(('exercise', 'shovel snow'))

min – Returns the item with the minimum value. Example:

to_do_tuple.min() # returns 'groceries'

max – Returns the item with the maximum value. Example:

to_do_tuple.max() # returns 'wash car'

Slicing

Ranges of list items can be accessed by slicing. This is done by indexing the list with a beginning index followed by a colon and an ending index. The beginning index is inclusive. The ending index is exclusive. Example:

Extract elements 2 – 3 which would be indexes 1 – 2.

extracted_list = to_do_list[1:3]

Using the original list, this returns: to_do_list[‘laundry’, ‘wash car’] which is:
[element 2, element 3] which is:
[index 1, index 2]

If the beginning index is not included, the slicing starts at the first element in the list.

extracted_list = to_do_list[:3] # returns to_do_list['groceries', 'laundry', 'wash car']

If the ending index is not included, the slicing ends at the last element in the list.

extracted_list = to_do_list[1:] # returns to_do_list['laundry', 'wash car', 'vacuum']

If neither the beginning index or ending index is included, the slicing starts at the first element and ends at the last element.

extracted_list = to_do_list[:] # returns to_do_list['groceries', 'laundry', 'wash car', 'vacuum']

More Python