Python: Get items at depth? (set library?)
- by Mark
I have a nested list something like this:
PLACES = (
('CA', 'Canada', (
('AB', 'Alberta'),
('BC', 'British Columbia' (
('van', 'Vancouver'),
),
...
)),
('US', 'United States', (
('AL', 'Alabama'),
('AK', 'Alaska'),
...
I need to retrieve some data out of it. If depth is 0 I need to retrieve all the countries (and their codes), if depth == 1, I need to retrieve all the states/provinces, if depth == 2 I need to retrieve all the cities... and so forth. Is there some set library for doing stuff like this? Or can someone point me in the right direction? I started coding up a solution only to realize it wouldn't work for levels deeper than 1 because you have to go in and out of each list...