0

it will display wrong wrong correct # if wrong order display wrong correct order display correct

i have tried using nested loop to compare the element but then the output display too many time which is not something i wanted

     elif first in secList : #if list1 item is in list 2 and order of item are not same

         print("wrong")
     else:

         print("nothing")
Jon Kim
  • 97
  • 1
  • 11
  • 1
    The reason you're getting three lots of print-outs is that you're comparing _every_ item in `list1` against _every_ item in `list2`, whereas you want to loop through both together at the same rate. The built-in `zip()` is perfect for exactly this - see the answer below! – Tim Aug 31 '19 at 16:45

1 Answers1

3

Try this :

>>> list1= ['black','red','blue']
>>> list2=['red','black','blue']
>>> print(*["correct" if i==j else "wrong" for i,j in zip(list1, list2)], sep='\n')
wrong
wrong
correct

This is equivalent to the following :

for i,j in zip(list1, list2):
  if i==j:
    print("correct")
  else:
    print("wrong")

or

for i, j in zip(list1, list2):
    print("correct" if i == j else "wrong")

Using zip function of python, you can aggregate items from multiple iterables.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56