#๐Ÿ”’ Updating a dictionary value by comparing values using a nested loop

19 messages ยท Page 1 of 1 (latest)

final echo
#

Hey there, I've been a bit stuck on getting my head around why my code here didn't work the way I created it.


# Combining two lists here which contain the status_pattern names for the keys and object UUID as values
status = {name: id for name, id in zip(status_list, status_id)}
payload = {"status": input("Status")} # input here will be: Active
for key, value in payload.items():
                match_status = status_pattern1.search(value, re.IGNORECASE)
                if match_status and value in match_status.group(0): # Here all goes well
                    for keys, val in status.items(): # My issue seems to be here. My intention is to compare the user value to the keys in status, where one of them is "Active". What I thought would happen is that the value in payload would then be updated to the corresponding value found in status. What did I do wrong here?
                        if value in keys:
                            payload["status"] = val```
swift raftBOT
#

@final echo

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

fading vortex
frosty needle
#

why do you need that second loop? You can simply do

if value in status:

to check if status has that key

final echo
final echo
frosty needle
#

can you provide sample of the list and id

final echo
fading vortex
frosty needle
#

so the status is list is an exact match. Why do you need re for that?

#

I think your variable name is confusing you... your status is holding {status: value}

#

while payload IS the status you want?

#

and then you loop payload, which is holding only 1 value.

#

this does not make sense

#

this really is all you need, with names that make sense imo

#
id_status = {
    'Active': 'AAA', 
    'Container': 'BBB',
    'Deprecated': 'CCC',
    'Reserved': 'DDD'}

input_status = input("Status")  # input "Active"

if input_status in id_status:
    id_status[input_status] = val```
swift raftBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.