Using tuples of items
What's the best way to represent simple (x,y) and (r,g,b) groups of values? How can we keep things that are pairs, such as latitude and longitude, together?
Getting ready
In the String parsing with regular expressions recipe, we skipped over an interesting data structure.
We had data that looked like this:
>>> ingredient = "Kumquat: 2 cups"
We parsed this into meaningful data using a regular expression, like this:
>>> import re
>>> ingredient_pattern = re.compile(r'(?P<ingredient>\w+):\s+(?P<amount>\d+)\s+(?P<unit>\w+)')
>>> match = ingredient_pattern.match(ingredient)
>>> match.groups()
('Kumquat', '2', 'cups')
The result is a tuple object with three pieces of data. There are lots of places where this kind of grouped data can come in handy.
How to do it...
We'll look at two aspects to this: putting things...