# What is the theoretical result of flattening a list containing only itself?

Consider the following python code

X = [None]
X[0] = X


This has many fun properties such as

X[0] == X


and

X[0][0][0][0][0][0][0][0] == X


I understand how this works from a practical standpoint, as it's all just the same pointer.

Now, when flattening a list, we tend to convert things from

[[1, 2, 3], [[4, 5], [6, 7]], 8, 9]


to

[1, 2, 3, 4, 5, 6, 7, 8, 9]


In this case, I am considering flattening to be reducing a multi-dimensional list down to a single list of only non-list elements.

In practice, flattening this list would be impossible, as it would create an infinite loop.

This may be more mathematical in nature, but I'm unsure how to put it in mathematical terms. Suppose we could flatten this list, would the result simply be the empty list?

For context, I initially got this idea by considering the list

X = [[], []]
X[0] = X


It is clear to see that at each pass of flattening, the empty list that is the second element simply disappears. This lead me to think that the overall result, may be the empty list.

Could it be possible that flattening this list would theoretically produce an infinitely long list of the list itself, as in

X == [X, X, X, X, X, X, ..., X]


This is purely a fun thought exercise. Any insight and discussion on this would be appreciated.

P.S. Although I'm looking for an answer in plain terms, if anyone is more mathematically inclined, I would be interested to see how this problem could be formulated in some sort of set notation. Please feel free to point me to a relevant math exchange thread as well.

P.P.S. I would also be interested in a solid proof (not formal) to go along with the answer.

From a mathematical point of view, this is impossible: there is no such a thing as a set containing itself (in normal logic sense) or a vector who has itself as an element, so this question doesn't really mean anything outside the realm of using pointers in a list

BUT, on a second thought: I might be able to formulate what "flattening" is if you allow me to define things however I want.

Lets say our world is some graph $$G$$, nodes in it would be elements, and an edge $$(v,u)$$ would mean that the element $$v$$ contains the pointer to $$u$$. Then, in those terms, the array [[5,6],3,[7]] might look like this:

Flattening the graph in this sense means to take every non-direct child of the root node (the array's node) and make it a direct child of it. Then, your array X looks like this:

And when you flatten it you would remain with the exact same graph, and therefore in this sense when you flatten X you would get simply X itself.

However, this is truly meaningless because it's only by the definition I just made up: you could have defined it in another way and it would have resulted in something else maybe!

• Regarding your first statement, Russell's Paradox only arises under "classical logic" - I think it's a bit harsh to limit mathematics to classical logic. – Novicegrammer Jul 2 at 23:35
• Obviously... but i wanted to point out that it is not properly defined within the logic we are used to work with. – nir shahar Jul 2 at 23:41