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
pear ['apple', 'pear'] ['chocolate', 'pear', 'apple']
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"))
0 2
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)
True False
Sorting Lists
List can be sorted with the .sort() method.print (shopping) shopping.sort() print (shopping)
['apple', 'pear', 'chocolate'] ['apple', 'chocolate', 'pear']
How to Copy a List
Lists can be copied with a blank index rangesorted_shopping = shopping[:] sorted_shopping.sort() print (shopping) print (sorted_shopping)
['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)
['apple', 'pear', 'chocolate', 'flour']
shopping[2] = "eggs" print (shopping)
['apple', 'pear', 'eggs', 'flour']
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)
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