-2

I'm scraping with python and I have a list made at most of two letters(R and D), where the content can be always the same (i.e. all the elements are R or alternatively D) or it can be that there are some D and some R. How can I get 1 if the list is made of either R only (or D only) and 0 if there are both D and R? Thanks in advance

1 Answers1

1

You can just check to see if all the elements are identical.

val = 1 if all(elem == items[0] for elem in items) else 0
wpercy
  • 9,636
  • 4
  • 33
  • 45
  • Thanks, it works well until I find some private informations on the website and so I've an empty list. Python gives me this error "TypeError: can only concatenate list (not "str") to list". I tried to set an exception when the length of the list is 0 with the command if but it doesn't fix the error. – Francesco Aug 05 '18 at 14:16