1

I am working on a python code to update list of dictionaries if input exists in list of dictionaries. If input doesn't exist in list of dictionaries it should print "value doesn't exist in entire list" or do some other operation. Below is the code I have written

a = [{'main_color': 'red', 'second_color': 'blue'},
 {'main_color': 'yellow', 'second_color': 'green'},
 {'main_color': 'blue', 'second_color': 'blue1'}]

conType = input('Enter main color: ')
color=input('Enter secondary color :')

conType1= input('Enter another main color: ')
color1=input('Enter another secondary color: ')

valueDict={}

if conType:
    valueDict[conType]=color

if conType1:
    valueDict[conType1]=color1

print(valueDict)

for d in a:

    for i,j in valueDict.items():

        if d['main_color'] == i:

            print('matched')

            d['second_color'] = j
            break
    else:

        print('no value')

print(a)

Below is the output When I tried to execute above code

Enter main color: red

Enter secondary color :black

Enter another main color: yellow

Enter another secondary color: white

{'red': 'black', 'yellow': 'white'}

matched

matched

no value

[{'main_color': 'red', 'second_color': 'black'}, {'main_color': 'yellow', 'second_color': 'white'}, {'main_color': 'blue', 'second_color': 'blue1'}]

Issue here is 'no value' is getting printed. In my use case no value shouldn't be printed at all.

I have been through Searching array reports "not found" even though it's found and https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops

I am not sure why "no value" is getting printed. I am looking for a way to avoid executing else block if all inputs are present in list of dictionaries. Please help.

Zik
  • 202
  • 3
  • 11
sudhir
  • 219
  • 5
  • 17

4 Answers4

0

you need to reverse the order of your loops.

a = [{'main_color': 'red', 'second_color': 'blue'},
 {'main_color': 'yellow', 'second_color': 'green'},
 {'main_color': 'blue', 'second_color': 'blue1'}]

conType = input('Enter main color: ')
color=input('Enter secondary color :')

conType1= input('Enter another main color: ')
color1=input('Enter another secondary color: ')

valueDict={}

if conType:
    valueDict[conType]=color

if conType1:
    valueDict[conType1]=color1

print(valueDict)


for i,j in valueDict.items():

    for d in a:

        if d['main_color'] == i:

            print('matched')

            d['second_color'] = j

            break
    else:

        print('no value')

print(a)

This works for me and does not print 'no value'

  • Nope. This is still printing – sudhir May 29 '19 at 08:48
  • This logic will fail if one input value exists in list of dictionaries and other input value doesn't – sudhir May 29 '19 at 09:01
  • That is because you have coded it that way, add an elif statement to check for second item in list or if you want both to match then write an if statement with 2 conditional clauses AKA item 1 in dict and item 2 in dict. Then if none of these are matching it will go to the else statement and print no value – Rahul Patel May 29 '19 at 12:43
0

for allow to use else command to use after it iteration complete. you are getting no value beacuse of that.

what you need to do is maintain a flag to capture if an macheded happened or not

example

for d in a:
    flag = False
    for i,j in valueDict.items():
        if d['main_color'] == i:
            print('matched')
            d['second_color'] = j
            flag = True
            # enter code here
            break
    if flag is False:
            print('no value')
sahasrara62
  • 10,069
  • 3
  • 29
  • 44
  • This is wrong. In this case no value will be printed multiple times – sudhir May 29 '19 at 08:46
  • This logic is failing when I added if flag is False: print('no value') for i,j in valueDict.items(): print(i) This is printing input multiple times. Please help – sudhir May 29 '19 at 09:47
  • logic is correct, do you understand what you did there? why won't this will print list multiple time because you used a `for loop` – sahasrara62 May 29 '19 at 09:52
  • I understood why it printed multiple times. Used your logic to fit my use case. But I am not able to. Enter main color: a Enter secondary color :b Enter another main color: c Enter another secondary color: d I tried with above input values. Code should print a,c only once. This is what I am trying to do. – sudhir May 29 '19 at 09:56
  • better if you ask your use case again as new question here in SO – sahasrara62 May 29 '19 at 09:57
0

Did I throw the baby out with the bathwater?

a = {'red': 'blue', 'yellow': 'green', 'blue': 'blue1'}
b = {'red': 'black', 'yellow': 'white'}

for i, j in b.items():
    if i in a:
        a[i] = j
print(a)

In Python 3.5 or greater this code (for loop) can be reduced to:

a = {**a, **b}

Alex Lopatin
  • 682
  • 4
  • 8
0

Agree with @Alexander, but you also need a variable to count the color in the list. with the count, you can reconstruct your array if there is duplicate in the list

a = {'red': 'blue', 'yellow': 'green', 'blue': 'blue1'}
# in case like a = [{'main_color': 'red', 'second_color': 'blue'},
# {'main_color': 'red', 'second_color': 'blue'}]
a_count = {'red': 1, 'yellow':1, 'blue': 1}
b = {'red': 'black', 'yellow': 'white'}

for i, j in b.items():
    if i in a:
        a[i] = j
print(a)
Zik
  • 202
  • 3
  • 11