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:

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

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.

Adding Items to a Python List

You can add items to a Python list using the append command.

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.

Check if Item is in a list

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.

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

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.

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.

Adding to and modifying items in a Python Dictionary

You can add items and modify items in the following manner:

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.

Other useful resources

  • There is a good introduction to lists on Tutorials Point but remember its written for Python 2.7.
About CDB 361 Articles
Self-Employed Software Developer, Spark, Property Management, Hobby Forestry, Ex-Teacher, Engineering - Wood, Metal, Electrics & Computers. Outdoors - Walk, Cycle, Kitesurf,

Be the first to comment

Leave a Reply

Your email address will not be published.


*


This site uses Akismet to reduce spam. Learn how your comment data is processed.