# Nesting Instinct

I figured I'd start a series of Python challenges in my blog. Whenever I've posted questions such as these in the past, the discussion that follows is always very entertaining!

A discussion arose today at work about the best way to flatten a tuple consisting of values and other tuples (which may be nested arbitrarily). So the tuple `(1, (1, 2, (1, 2, 3), 3))` would become `(1, 1, 2, 1, 2, 3, 3)`. Now there was actually code implemented which was functional, efficient and easy to read – but where is the fun in that?

I figured I could re-write it in a more terse manner, and here is what I came up with:

```def flatten(tpl):
return eval(repr(tpl).replace('(', '').replace(')', ''))

t= (1, (1, 2, (1, 2, 3), 3))
print flatten(t)
```

This `flatten` function would actually work in the context of our app, but I would never use it in production code. My challenge today, is to tell me why this code should never be used!

Use Markdown for formatting
```*Italic* **Bold** `inline code`

> This is a quote
>

```python
import this
```
```
your comment will be previewed here pythonista
You could have string values (for example) with parenthesis in them. These values would be changed. Steve Holden
A further reason is that you have no idea what the repr of the object looks like, since you don't even examine its type …

One must hope nobody starts devising malicious objects with exploitive repr()s.

Cute idea, though. Euan Goddard
I can't believe you had the audacity to post that! Very amusing though. Let me know when you can properly do it in 2 lines WIll McGugan

``` def flatten(tpl):
return sum(map(flatten, tpl), ()) if type(tpl) is tuple else (tpl,)

t= (1, (1, 2, (1, 2, 3), 3))
print flatten(t)
```

Not quite as concise as the the other flatten, but less of a crime against Python! jdm
2euan: is this “properly”?
``` def flatten(tpl):
return reduce(lambda x,y: x+flatten(y),tpl,()) if type(tpl) is tuple else (tpl,)
``` Roberto
A safe variant of your solution, assuming the values are integers:

def flatten(tpl):
return tuple( map(int, repr(tpl).replace('(', ‘').replace(’)', ‘').split(’, ')) ) Roberto
Posting again, sorry:

``` def flatten(tpl):
return tuple( map(int, repr(tpl).replace('(', '').replace(')', '').split(', ')) )
``` Since I've been glancing at the Y combinator

``` >>> t= (1, (1, 2, (1, 2, 3), 3))
>>> _smash = lambda f: lambda t: sum(map(f, t), ()) if type(t) is tuple else (t,)
>>> Y = lambda f: (lambda x: x(x))(lambda y: f(lambda z: y(y)(z)))
>>> # From two non recursive functions:
>>> smash = Y(_smash)
>>> smash(t)
(1, 1, 2, 1, 2, 3, 3)
>>>
```

Remember: Don't do this at home folks :-) apoirier
One liner variation, working with any iterable:

flatten = lambda t: sum(map(flatten, t), ()) if hasattr(t, ‘__iter__’) else (t,) Eno

``` from itertools import chain
flatten = lambda x :chain(*(flatten(v) if type(v) is tuple else (v,) for v in x))
t = (1, (1, 2, (1, 2, 3), 3))
print list(flatten(t))
```
I recommend a visit to http://www.challenge-you.com if you enjoy those kind of challenges.