Lists

Overview
Lists are sequences that remain in order. These can be built up of any data type available and can even be lists of lists.
Lists are notated with square brackets around the list, which commas separating items.
customer_basket = ["apple","pear","pear"]

customer_sale = [customer_basket, True, "01/07/2017"]

Indexing and Slicing Lists

Once a list variable has been created, it can be indexed just like a string. This is done with square brackets after the variable. There are 3 parameters that can be included in the square brackets, start, end and step.

shopping = ["apple", "pear", "chocolate"]

print (shopping[1]) # print item at index 1
print (shopping[0:2]) # print the first 2 items of a list
print (shopping[::-1]) # print the entire list, but going backwards
Returns:
pear
['apple', 'pear']
['chocolate', 'pear', 'apple']
Note that if a variable is missing it is set to the default value. start is set to start of the list, end is set to end of the list and step is set to one.

Search a list for an Item

Lists can be searched to check the location of an item in a list, this can be done with index().
print (shopping.index("apple"))
print (shopping.index("chocolate"))  
Returns:
0
2
An error will be raised if the item is not in the list.
print (shopping.index("cookie"))
ValueError: 'cookie' is not in list

Lists can also be search to confirm if an item is in the list, without returning a location.
print ("apple" in shopping)                      
print ("cookie" in shopping)
Returns:
True
False

Sorting Lists

List can be sorted with the .sort() method.
print (shopping)
shopping.sort()
print (shopping)
Returns:
['apple', 'pear', 'chocolate']
['apple', 'chocolate', 'pear']
Notice that this method changes the list in place. This is that in python lists are mutable. If you don't want to change the original list we need to make a copy of the list.

How to Copy a List

Lists can be copied with a blank index range
sorted_shopping = shopping[:]
sorted_shopping.sort()
print (shopping)
print (sorted_shopping)
Returns:
['apple', 'pear', 'chocolate']
['apple', 'chocolate', 'pear']

Adding and Removing Items from a List

To add an item to a list we can use append(), this will add the appended item to the end of the list.

shopping.append("flour")
print (shopping)
Returns:
['apple', 'pear', 'chocolate', 'flour']
Values in a list at a set location can also be redefined.
shopping[2] = "eggs"
print (shopping)
Returns:
['apple', 'pear', 'eggs', 'flour']
Items can be removed from a list through 3 methods, del, remove and pop.
del - removes an item at a set index
remove - removes the first matching value
pop - removes an item at a given index, but it also returns this item
del shopping[0]
shopping.remove("eggs")
item = shopping.pop(1)
print (item)
print (shopping)
Returns:
flour
['pear']



Related Sections

Python - Learn the python from the basics up. This fast track example code course will get you creating powerful python programs in no time.
Tkinter - Learn the key features of the tkinter to allow you to create user interfaces for your python programs.


Related Pages

Pickle - The pickle library allows a program to save data in a binary file.
Python - List Comprehension - Create powerful list comprehension expressions.
Python - Generators - Learn about generator statements.
Python - Data types - The learn about the key datatypes in the python language.
Tkinter - Combo Box - This is a short example showing how to use combo boxes in a GUI.
Tkinter - Radio Buttons - A short example of how to create a GUI with radio buttons.
Tkinter - Canvas - Canvas allows for a range of shapes to be plotted, such as circles, squares, lines