Let's say you have a list that is made up of items, some of which are lists. Each of those lists may be made up of items or more lists and so on...
For this challenge, your job is to take a list
In designing your functions, some of you may want to focus solely on speed, others on creativity, and some of you may want to find a balance of the two... all of these approaches are great. Let's see how many different approaches the Python board can come up with!
Example input:
>>> x = [1,[2,[3,[4,5,6,[7]],9]],[10,11],12]
>>> flatten(x)
>>> print(x)
[1, 2, 3, 4, 5, 6, 7, 9, 10, 11, 12]
Can anyone flatten this list?
x = [1,[2,[3,[4,5,6,[7]],9]],(10,11),12]
OPTIONAL:
If you're feeling particularly hardcore, let's assume that the initial value is a list or tuple, but it can also contain a dictionary. For dictionaries, order the values based on the keys (0-9, A-Z, a-z case sensitive). They keys should not exist in our final result, only their associated flattened values.
This post has been edited by atraub: 06 January 2011 - 08:01 AM
Reason for edit:: removed tuple input

New Topic/Question
Reply




MultiQuote






|