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
James Reid
  • 535
  • 1
  • 5
  • 11

1 Answers1

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
Prajwal K R
  • 682
  • 5
  • 14
Aswin Murugesh
  • 10,831
  • 10
  • 40
  • 69
  • 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" – James Reid Sep 30 '15 at 04:08
  • @FredGarcia http://stackoverflow.com/questions/2468804/django-template-call-function – Aswin Murugesh Sep 30 '15 at 05:38