1
daf = [2,2]

if all values in the array are the same

       print ("error")
else

       print(daf.index(max(daf)))

I don't know how to write the if statement for the above code since

daf = [2,2]

print(daf.index(max(daf)))

just returns 0 which is also the first index of the array

Ruijie Lu
  • 17
  • 5
  • 1
    `if len(set(daf)) == 1:`? If the size is always 2, why not just `if daf[0] == daf[1]`? How is your question about `max` function? Your question is how to find if all elements in a list are equal, and if you would google exactly that - you would find your solution – Tomerikoo May 24 '20 at 20:16

2 Answers2

4

For the if statement, you can use set which returns a list of all the unique values. If len == 1 then there was only one unique value.

if len(set(dat)) == 1:
   print('error')

Anna Nevison
  • 2,709
  • 6
  • 21
1

You can use the python's all function to do this:

if all(x == daf[0] for x in daf):
  print("error")
tanmay_garg
  • 377
  • 1
  • 13