Structuring Data in Python (list from lists?)
Question: I'm currently developing a script in Python 2.7 which pulls approximately 50
string values from a URL and places these values in a list.

For each of these 50 values, they each have a further respective 20 associated
string values.

So for example, I may capture two initial values from a URL which are "Ford"
and "BMW". I will then requery "Ford" to find associated models such as
"Focus" and "Mondeo" etc....

Eventually I need to join those values to carry out a further URL query (ie.
www.ford.com/fiesta and www.ford.com/mondeo and www.bmw.com/xxxx)

I am wondering what the best was is to structure my data? I am thinking of
creating a list of parent values and then for each parent value, create a sub-
list titles the parent value with its associated values in the list.

Is there a better way to structure the data?


Answer: A friendly advice: keep things simple. Refer to the Zen of Python when in
doubt: <http://www.python.org/dev/peps/pep-0020/>

"Simple is better than complex."

I would go for a dict of lists, or even better, a `defaultdict`
(<http://docs.python.org/2/library/collections.html#collections.defaultdict>):

    
    
    from collections import defaultdict
    
    
    cars = defaultdict(list)
    
    cars["Ford"].append("Mondeo")
    cars["Ford"].append("Focus")
    cars["BMW"].append("SuperCoolModel")
    
    print cars
    

Gives:

    
    
    defaultdict(<type 'list'>, {'BMW': ['SuperCoolModel'], 'Ford': ['Mondeo', 'Focus']})
    

