banner

© 2004-19 ict4u.net

Python Data Structures

Python has a rich and varied collection of data structures which will make your life as programmer simpler once you have got your head round them!

The best starting point is the List

Python lists are just like arrays in other languages. Lists are used to store multiple values in one data structure.
Create an list, and assign values to it like this:

listFruit = ["Orange", "Apple", "Pear", "Mango"]
					

Lists are referenced using a numerical index starting a zero for each element in the list

You can use the normal array operations (loops, element numbers etc) to reference individual elements, including assigning it to be another array to make up a multi dimensional list

Here are the methods that can be used on lists:

Method Description
aList.append(obj) Appends object obj (a simple variable) to aList
aList.count(obj) Returns count of how many times obj occurs in aList
aList.extend(seq) Appends the contents of seq (where seq is another list)to aList
Note:Lists can be appended by simply adding them together:
listThree = listOne + listTwo
aList.index(obj) Returns the index number corresponding to the first occurrence of obj
aList.insert(index, obj) Inserts object obj into aList at offset index
aList.pop(indexNo) Removes and returns last object or indexNo if specified, from aList
aList.remove(obj) Removes object obj from aList
aList.reverse() Reverses objects of aList in place
aList.sort([func]) Sorts objects of aList, use compare method if given
See here sorting a nested list
arithmetic using builtin functions Max, Min Count
aList.clear() removes all the elements of a list. Alternatively aList = [] which creates a new instance of aList

Check out this presentation for a summary of what lists are and how to use them: Python Basics Presentation
There is also a set of exercises on 1D and 2D lists to help you get familiar with coding solutions requiring lists.

The methods in the table below will help you understand how to use them in your code

Method Description
Basic list setup Code to setup a simple list and then append data to it
Initialise lists 3 ways to initialise a 1d ist
2D List setup and print Setup a small 2D list with numbers and print out using nested for loops
List Methods Common methods such as reverse, sort and append
List Append (push) Adds an element to the end of a list
List Index Finding an element in a list. Counting the number of occurrences
List remove (pop) Removing data from a list
Display List Item Use an index number to display a list item
2D List Processing Skeleton Code to create an empty 2D list

Notes: Has an asterisk to denote where the cells are

2D List (Variable sized) Initialise a variable sized 2D list using a comprehension

Notes: Uses for loops to control the number of rows and cols in the matrix

Finding data in a 2d list Finding an element in a 2d list. Returns a list of indices where the string was found

Notes: only works with strings

String to List Create a list from a word or sentence and reverse back from a list into a word or sentence
Largest number in list Starter code to find the largest number in a list