Arrays & Lists
Python provides a number of array like structures that you can use. Python Lists are probably more flexible and the easiest to use. They are able to contain data of different types and can grow/shrink in size easily. However, the Array datatype takes up less space but is of fixed size and single data type. Python also provides Dictionaries, which let us store values linked to a specific key. There are other data structures available but they are beyond the scope of this page.
The following explanations are based on Python 3 although should work for Python 2 as well.
Lists
Declaring a Python List
We create lists in Python using square brackets. Here is a simple example:
words = ["bread","and","butter","pudding","tastes","lush"]
How long is my Python List?
You can then find out how many items are in the list or its length with the len method
len(words)
Accessing items in a Python List?
Because we start counting arrays and lists in Python from zero.
0 | 1 | 2 | 3 | 4 | 5 |
bread | and | butter | pudding | tastes | lush |
If you wanted to get the word in position 3 you would use the index syntax as shown below which would return the word pudding.
words[3]
Adding Items to a Python List
You can add items to a Python list using the append command.
words.append("Smash")
Looping through items in a Python List
If you wanted to loop through all the items in a list you can use a for loop e.g.
words = ["bread","and","butter","pudding","tastes","lush"] for word in words: print (word)
Check if Item is in a list
word = "smash"
if word in words:
return True
Other useful resources
- There is a good introduction to lists on Effbot.
- Also check out TutorialsPoint although it is written for Python 2.7 so all the prints should be updated to include (brackets).
Dictionaries
A Python dictionary is an unsorted list of key value pairings. Its important to note that even if you put the items into a dictionary in a specific order they will not be returned in that order.
Creating a dictionary in Python.
To create a dictionary in Python we use curly brackets and colons to seperate the key from a value.
scores = {"John":23, "Annie":12, "Tony":19}
How long is my Python Dictionary?
You can then find out how many items are in the list or its length with the len method
len(scores)
Accessing items in a Python Dictionary?
There are two methods for accessing a value in a dictionary. The first is the same as with a list e.g.
scores["John"]
However, the problem here is that if the key (John) doesn’t exist Python will produce a KeyError. The more secure method for accessing values based on their keys is to use the get command, which will return False if that key is not found.
scores.get("John")
Adding to and modifying items in a Python Dictionary
You can add items and modify items in the following manner:
scores['newplayer"] = 34
scores["John"] = 44
Looping through items in a Python Dictionary
If you wanted to loop through all the items in a list you can use a for loop e.g.
scores = {"John":23, "Annie":12, "Tony":19} for key, value in scores.items(): print(key, value)
Other useful resources
- There is a good introduction to lists on Tutorials Point but remember its written for Python 2.7.