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
Asked
Active
Viewed 36 times
-2
-
Is this homework? If so, you should say so. In any case, post samples of what you have already tried. – manassehkatz-Moving 2 Codidact Aug 02 '18 at 17:46
-
`len(set(my_list)) % 2` – L3viathan Aug 02 '18 at 17:51
-
Yes, sorry...it is homework. Thanks – Francesco Aug 05 '18 at 14:16
1 Answers
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