0

When I index new objects into my list, the newest added object copies to all the previously added objects of the list. I have a small Python program running on a RPI3 taking input from 4 buttons.

    full = []
    schedule = ['day', 'activity', 'clock']
    n = 4
    def setDay(x):
        while True:
            input_state18 = GPIO.input(18)
            input_state23 = GPIO.input(23)
            input_state24 = GPIO.input(24)
            input_state25 = GPIO.input(25)
            if input_state23 == False:
                    print('Monday')
                    time.sleep(0.2)
                    x[0] = 'Monday'
                    break
            if input_state18 == False:
                    print('Tuesday')
                    time.sleep(0.2)
                    x[0] = 'Tuesday'
                    break
            if input_state24 == False:
                    print('Wednesday')
                    time.sleep(0.2)
                    x[0] = 'Wednesday'
                    break
            if input_state25 == False:
                    print('Thursday')
                    time.sleep(0.2)
                    x[0] = 'Thursday'
                    break


    def setActivity(x): 
#same as setDay
    def setClock(x):    
#same as setDay                     

    if __name__ == "__main__":
        i = 0
        while i < n:
            setDay(schedule)
            setActivity(schedule)
            setClock(schedule)
            full.insert(i, schedule)
            i += 1
        #put schedule into first spot in total list 
    print(full)

When I run the program I get the following output into my terminal:

Monday
Study
19:00
Tuesday
Training
17:00
Monday
Swimming
19:00
Thursday
Training
17:00
[['Thursday', 'Training', '17:00'], ['Thursday', 'Training', '17:00'], ['Thursday', 'Training', '17:00'], ['Thursday', 'Training', '17:00']]

So the problem is located around my while loop in the main function. I tried using list.append as well but I got the same output. How do I change my while loop so the added lists don't replace the previous?

Chen A.
  • 7,798
  • 2
  • 26
  • 48
Daniel
  • 351
  • 2
  • 14
  • Appending `schedule` does not create a copy; you re-use the same list over and over. Create a *new* list each iteration (move `schedule = [...]` into the loop) or create a copy (see the duplicate). – Martijn Pieters Sep 24 '17 at 20:42

2 Answers2

0

Lists are mostly handled using references, thus appending will not copy the schedule, it will just append it's reference.

A trick about this is using the module copy.

import copy

full.insert(i, copy.deepcopy(schedule))

This will still append a reference, but a reference to the copied object. This should probably help you in this case

SMFSW
  • 286
  • 1
  • 2
  • 10
0

There is a lot of cleanup required in this code. But the easiest way right now to make it run so that you get the desired output is:

 while i < n:
        # Defined within the while loop
        schedule = ['day', 'activity', 'clock']
        setDay(schedule)
        setActivity(schedule)
        setClock(schedule)
        full.insert(i, schedule)

Because, right now you are passing the same reference of the list to functions again, and appending the same list to the full list. Instead, you need new list element each time which you should be adding to the full.

Anonymous
  • 40,020
  • 8
  • 82
  • 111