0
_read_ = {}
_read_['time_list'] = [6000, 4000, 2000]
def abc(_read_): 
    time_list = _read_['time_list'] 
    for i in range (0, 10):
        if len(time_list) != 0:
            print (time_list.pop())
        else:
            time_list = _read_['time_list'] 

abc(_wear_)

I'm trying to reinitialize the list(line 10), when it gets empty, with the same values as its being done in line 5 I was expecting the print to be

2000
4000
6000
2000
4000
6000
2000
4000
6000
2000

but I don't get anything after the first series is popped out i.e. the list does not get reinitialized

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 3
    Possible duplicate of [How to clone or copy a list?](https://stackoverflow.com/questions/2612802/how-to-clone-or-copy-a-list) – Aran-Fey Sep 15 '17 at 07:30
  • An assignment doesn't make a copy. – Aran-Fey Sep 15 '17 at 07:30
  • Because in Python `time_list` is reference of `_read_['time_list']`, so when you change `_read_['time_list']` it also changes the value of `time_list` and vice versa – Murtuza Z Sep 15 '17 at 07:31
  • First why there is a loop from `(0, 10)`, and you popping all the elements of `time_list` out and when the length is zero, re init by `_read_['time_list']`. What are trying to achieve here. – bhansa Sep 15 '17 at 07:34
  • Thanks @n33rma. Yes, that's the correct reason. I fixed it by using time_list = _read_['time_list'].copy() function. I cannot write the whole context due to copyright issues but then the summary is, the code reinitializes the list, whenever it will gets empty with original values. – Nitin Mathur Sep 15 '17 at 13:55

0 Answers0