1

I tried to create a problem like [0,1,2,[0,1,2,[0,1,2,[0,1,2] ] ]... by using recursion, but it got some bugs that I don't know where it came from.

It creates an output like this:

[1, 2, 3, 4, 5, [...]]

So let's make an example, x = [1,2,3,4,5] and len_ = len(x) for the recursion. The code will be like this:

list_ = [1,2,3,4,5]
n = len(list_)
dull = []
def BlastedField3(n):
    global list_
    list_ += [list_]
    if n == 1:
        return list_
    if n > 1:
        return list_
    return BlastedFild(n-1)
print(BlastedField3(n))

So I was imagining that list_ += [list_] was something like per each recursion of the function the list_ += [list_] working too. But! It works, but the bug [...] is coming up next to it.

Is there a way to solve this without this bug [...] or just try a loop?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
XXXXXX
  • 21
  • 3
  • You are creating a list that contains itself (a reference to itself). The [...] is Python's way of displaying that, as obviously the "real" representation is infinite. – user2390182 Sep 01 '21 at 10:32
  • Can you give me where to learn this "reference to itself"? I want to make this real without using much loops. – XXXXXX Sep 01 '21 at 10:39
  • You should use `list_ += list([list_]) ` rather than just `list_ += [list_] `. The extra `list( )` is there to explicitly make a copy. If you don't make a copy, you're using *literally* the same list object, the infamous "reference to itself" – Stef Sep 01 '21 at 12:05
  • Also I recommend avoiding the keyword `global` at all cost. It will be a source of more errors than you can imagine and there really is no good reason to use it in these kind of problems. – Stef Sep 01 '21 at 12:06
  • Related questions: [Why does += behave unexpectedly on lists?](https://stackoverflow.com/questions/2347265/why-does-behave-unexpectedly-on-lists) and [Changing one list unexpectedly changes another, too](https://stackoverflow.com/questions/29785084/changing-one-list-unexpectedly-changes-another-too) – Stef Sep 01 '21 at 12:09
  • Reference question: [List changes unexpectedly after assignment, why is this and how can I prevent it?](https://stackoverflow.com/questions/2612802/list-changes-unexpectedly-after-assignment-why-is-this-and-how-can-i-prevent-it) – Stef Sep 01 '21 at 12:11

1 Answers1

1

The problem of creating a self-containing list can be solved by not using references but shallow copies and by not reusing global variables (whenever you feel the need for those, look at your design long and hard).

base = [1,2,3,4,5]

def blasted(n):
    if n <= 1:
        return base[:]  # shallow copy to never accidentally expose your base
    return base + [blasted(n-1)]
    
>>> blasted(1)
[1, 2, 3, 4, 5]
>>> blasted(2)
[1, 2, 3, 4, 5, [1, 2, 3, 4, 5]]
>>> blasted(3)
[1, 2, 3, 4, 5, [1, 2, 3, 4, 5, [1, 2, 3, 4, 5]]]

Actually the use of shallow copies is not strictly necessary as the code never mutates the base list. But I would still recommend it because if someone were to mutate any result of the blast, base might be affected.

user2390182
  • 72,016
  • 6
  • 67
  • 89