0

I am completely lost here. I made some code illustrating the problem:

_list_ = [1, 2]


def function(thing):
    return_list = []
    thing1 = thing

    for x in range(2):
        thing1[x] = 2
    return_list.append(thing1)

    thing1 = thing

    for x in range(2):
        thing1[x] = 1
    return_list.append(thing1)

    return return_list


print(function(_list_))

prints:

[[1, 1], [1, 1]]

When I want it to print:

[[2, 2], [1, 1]]

I have got no idea why this is happening, and help is appreciated.

Andronicus
  • 25,419
  • 17
  • 47
  • 88
  • 1
    also, do not name your variable like `_list_`, by which I mean neither use the underscores nor use the the word `list`. leading underscores are used to indicate internal members and `list` is a reserved type in python – syfluqs Feb 29 '20 at 15:59
  • I will refrain from doing so in the future, sorry :( – Max Armstrong Feb 29 '20 at 16:04

1 Answers1

2

It's because you have used the variable name thing1 twice, which means you're reusing that object. You would have to rename the second list for your code to work:

def function(thing):
    return_list = []
    thing1 = [0 for _ in range(len(thing))]

    for x in range(2):
        thing1[x] = 2
    return_list.append(thing1)

    thing2 = [0 for _ in range(len(thing))]

    for x in range(2):
        thing2[x] = 2
    return_list.append(thing2)

    return return_list

P.S.: I strongly suggest using better names for variables. thing1 and thing2 are not very informative.

Andronicus
  • 25,419
  • 17
  • 47
  • 88
  • I tried using this for the function, but it still printed [[1, 1], [1, 1]]. Also, sorry for the variable names! In my project they are different but I made separate code to demonstrate the problem since the original was full of unnecessary stuff – Max Armstrong Feb 29 '20 at 16:03
  • 1
    @MaxArmstrong yes, sorry for that, I've corrected the code. Now it works as you expected – Andronicus Feb 29 '20 at 16:04
  • My question was slightly misleading, here is a more in-depth version. The problem is that I can't simply add the numbers 2 and 1 (in my bad older version) to a variable to be printed, because the numbers in question depend on user input in the beginning of the program. – Max Armstrong Feb 29 '20 at 16:42
  • I wanted to, but there's a 90 minute delay on asking questions so I thought it would be simpler to change this one... Was it not? – Max Armstrong Feb 29 '20 at 17:01
  • @MaxArmstrong I didn't want to offend you, just imagine you're failing an exam, because there was something else to the question, that wasn't mentioned;) consider reverting the changes and creating another answer, I'll surely remove the downvote then! – Andronicus Feb 29 '20 at 17:28
  • sorry dude, I totally forgot what the original post was... I'll mark yours as correct again, since it was to the original question, and I've asked the new one separately now. Good day! – Max Armstrong Mar 01 '20 at 06:47
  • @MaxArmstrong no problem, you can always revert the question - just click edit a pick a version you want to go back to. I'll do that for you, good day to you too! – Andronicus Mar 01 '20 at 07:13