-1

Update: I simplified the example and added expected result

I'm making a mistake, and can't figure it out.

When I run this, my expectation is that the initial_payload will contain the first set of ['users'] from users_to_survey, since i'm appending them before overwriting, and then when I overwrite the product_payload['users'], the reminder_payload will contain the ['users'] from users_to_remind.

But when i print initial_payload at the end of this, it is identical to reminder_payload. The ['users'] for both is the same

initial_payload = []
reminder_payload = []

product_payload = {
    'product': 'colgate',
}

users_to_survey = ['kevin', 'dan']

if users_to_survey:
    product_payload['users'] = users_to_survey
    initial_payload.append(product_payload)


users_to_remind = ['bill', 'tom']

if users_to_remind:
    product_payload['users'] = users_to_remind
    reminder_payload.append(product_payload)

print(initial_payload)
print(reminder_payload)

I'm sure I'm missing something basic.

Brenden
  • 8,264
  • 14
  • 48
  • 78
  • 1
    `users_to_survey = product.get_users_to_survey` should have probably been a call? And the same for `users_to_remind`? – dedObed Jan 07 '20 at 21:08
  • 3
    Can you please make a simpler example? These variable names and reassignments are hard to follow (which is probably the cause of the problem). Also please add actual inputs so that this is reproducable. – mkrieger1 Jan 07 '20 at 21:10
  • 2
    @dedObed If that were the problem he'd get an error when he tries to slice it. – Barmar Jan 07 '20 at 21:11
  • 1
    Your code looks right and if there is no exception, you need to debug yourself. – Maurice Meyer Jan 07 '20 at 21:13
  • 2
    You are using the same `product_payload` dictionary in both `if` blocks. Is this intentional? – mkrieger1 Jan 07 '20 at 21:15
  • 3
    Maybe you are overwriting ```product_payload``` – Andrex Jan 07 '20 at 21:17
  • Sorry all, I've simplified the example. – Brenden Jan 07 '20 at 21:27
  • Yup, you're just changing the value of the same key. – AMC Jan 07 '20 at 21:30
  • Now you should also say what you had expected as result instead of what you actually get. – mkrieger1 Jan 07 '20 at 21:32
  • But anyway, it is essentially a duplicate of https://stackoverflow.com/questions/21987349/when-i-assign-a-dict-to-another-variable-why-does-python-update-both-dicts, and linked questions. – mkrieger1 Jan 07 '20 at 21:42

1 Answers1

0

Really unsure what you are trying to accomplish, but I can tell you why you are seeing what you are seeing. Since you create a dictionary called product_payload, if your current product has both users to survey and users to remind, then you will overwrite the object reference when you write it out.

Create two product_payloads, one for each of users to survey and users to remind. Try this

initial_payload = []
reminder_payload = []

product_payload_survey = {
    'product': 'colgate',
}
product_payload_remind = {
    'product': 'colgate',
}

users_to_survey = ['kevin', 'dan']

if users_to_survey:
    product_payload_survey['users'] = users_to_survey
    initial_payload.append(product_payload_survey)


users_to_remind = ['bill', 'tom']

if users_to_remind:
    product_payload_remind['users'] = users_to_remind
    reminder_payload.append(product_payload_remind)

print(initial_payload)
print(reminder_payload)

Try running the code snippet below to understand the error I am talking about.

class product:
    def __init__(self):
        self.get_users_to_survey = [1,2,3]
        self.get_users_to_remind = [4,5]
    BATCH_SIZE = 3
products = [product()]

initial_payload = []
reminder_payload = []

for product in products:

    product_payload = {
        'product': product,
    }

    users_to_survey = product.get_users_to_survey

    if users_to_survey:
        product_payload['users'] = users_to_survey[:BATCH_SIZE]
        product_payload['type'] = 'initial'
        initial_payload.append(product_payload)

    print(initial_payload)

    users_to_remind = product.get_users_to_remind

    if users_to_remind:
        product_payload['users'] = users_to_remind[:BATCH_SIZE]
        product_payload['type'] = 'reminder'
        reminder_payload.append(product_payload)

    print(initial_payload)
cmxu
  • 954
  • 5
  • 13