0

i would Like to display the word 'conflict' if the value is more than one in the list. this is my code

list = ['aa','bb','cc','aa']

conf = [s for s in list]

for a in list:
    if len(a in conf) > 1:
        print a, "-conflict"
    else:
        print a

I think my syntax is wrong in if len(a in conf)> 1: Please help me.

I am expecting this result:

aa - conflict
bb
cc
aa - conflict

1 Answer 1

1

You can use the count function.

if conf.count(a) > 1:
    print a, "-conflict"

The above method is similar to what you have tried. But this is inefficient when the list is large. So, use collections.Counter.

from collections import Counter
occurences = Counter(conf)
for a in list:
    if occurences[a] > 1:
        print a, "- conflict"
    else:
        print a
Sign up to request clarification or add additional context in comments.

2 Comments

Hi thanks.... I just want to ask because I will put this codes in html. it worked in shell however when I put this in my template. I got error. {% if conf.count(a) > 1%}. and it would show an error message "Could not parse the remainder"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.