## Data Structures

### Nested Lists and Dictionaries

In research programming, one of our most common tasks is building an appropriate structure to model our complicated data. Later in the course, we’ll see how we can define our own types, with their own attributes, properties, and methods. But probably the most common approach is to use nested structures of lists, dictionaries, and sets to model our data. For example, an address might be modelled as a dictionary with appropriately named fields:

UCL={'City': 'London',
'Street': 'Gower Street',
'Postcode': 'WC1E 6BT'}

James={
'City': 'London',
'Street': 'Waterson Street',
'Postcode': 'E2 8HH'
}


A collection of people’s addresses is then a list of dictionaries:

addresses=[UCL, James]

addresses


[{‘City’: ‘London’, ‘Postcode’: ‘WC1E 6BT’, ‘Street’: ‘Gower Street’},

{‘City’: ‘London’, ‘Postcode’: ‘E2 8HH’, ‘Street’: ‘Waterson Street’}]

A more complicated data structure, for example for a census database, might have a list of residents or employees at each address:

UCL['people']=['Clare','James', 'Owain']

James['people']=['Sue', 'James']

addresses


[{‘City’: ‘London’,

‘Postcode’: ‘WC1E 6BT’,

‘Street’: ‘Gower Street’,

‘people’: [‘Clare’, ‘James’, ‘Owain’]},

{‘City’: ‘London’,

‘Postcode’: ‘E2 8HH’,

‘Street’: ‘Waterson Street’,

‘people’: [‘Sue’, ‘James’]}]

This structure is thus a list of dictionaries, the values in which are either strings or lists.

We can go further, e.g.:

UCL['Residential']=False


And we can write code against our structures:

leaders =  [place['people'][0] for place in addresses]