1

I am pretty new to Python and coding in general.

I tried appending a sublist into a list. I was initially thrown off since it seems the list appended all the values I wanted, but the values were the final value of [2,1].

With the following code

next_state = [0, 0]

def create_a_path(current_state):
    possible_moves = [(2, 0), (1, 0), (1, 1), (2, 0), (1, 0)] 

    state_list = []
    for pm in possible_moves:
        #next_state = [0, 0]  # this solves the issue
        next_state[0] = current_state[0] + pm[0]
        next_state[1] = current_state[1] + pm[1]
        print(pm)
        print(next_state)
        state_list.append(next_state)
    return state_list

    
current_state = [1, 1]
print(create_a_path(current_state))

I would think that my sublist would be appended and the sublist would be updated once there is another loop, then append without overwriting the previous appended value.

The simple change of code lines (as commented) solved this issue, but I am confused why.

Could anyone explain to me why this is? Maybe I am missing something fundamental about append, loops, or more? Thank you in advance.

ghchoi
  • 4,812
  • 4
  • 30
  • 53
  • 2
    [**We don't allow images of text (code/input/output/errors, or otherwise) on Stack Overflow**](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question). Please post all text into the question with correct [formatting](https://stackoverflow.com/editing-help). Questions with images of text/code/errors are routinely closed. Please also check out the [tour](https://stackoverflow.com/tour) and the [question guide](https://stackoverflow.com/help/how-to-ask) to make sure this & your future questions are suitable for this Q&A. – costaparas Jan 30 '21 at 04:13
  • 1
    In the first image you declared next_state outside of the loop. Therefore it wasn't being reset every iteration. In the second one, you reset next_state to [0, 0] every single time your loop ran. – ArjunSahlot Jan 30 '21 at 04:19
  • To extend a list with elements from another iterable(list, tuple, string, etc.) you can use ```extend```. ```list.extend(tuple)```. – ArjunSahlot Jan 30 '21 at 04:21
  • Both of the results are the same. What is your desirable output? – ghchoi Jan 30 '21 at 05:09
  • @costaparas Sorry for the ignorance. I will look into the tour before I post next time. – FirstYearCoder Jan 30 '21 at 05:13
  • @SaladHead So, if it does not get reset, that means the same next_state is being appended. But if gets reset in the loop, it creates different instances of next_state therefore, the values are unique? Is this correct? My Nomenclature might not be very accurate in my explanation. I apologize. – FirstYearCoder Jan 30 '21 at 05:24
  • In the first case you're using the global next_state which references an object. The for loop in updating next_state is modifying this one object. The append is just appending references to this one object. Since all references are to the same object, the final list all has the same value. In the second case, you are making a local next_state in the for loop. Since all the objects are different with each loop, the final list has all different objects. – DarrylG Jan 30 '21 at 05:24
  • @DarrylG this is exactly what I needed to hear! Thank you for your explanation! – FirstYearCoder Jan 30 '21 at 05:26
  • @FirstYearCoder--this issue comes up in various forms such as [Why is this python generator returning the same value everytime?](https://stackoverflow.com/questions/31687185/why-is-this-python-generator-returning-the-same-value-everytime). – DarrylG Jan 30 '21 at 05:31

0 Answers0