1

How can I check if the letters of a word that the user entered are all in a list? This doesn't seem to work:

letters = ["a","b","c","d"]
inpt = str(raw_input())
if list(inpt) in letters:
    print "letters are in list"
else:
    print "letters are not in list"
Lara
  • 21
  • 9

2 Answers2

4

Use sets; either test for an intersection or for a subset:

if set(inpt) & set(letters):
    # *some* letters are in inpt

or

if set(letters) <= set(inpt):
    # *all* letters are in inpt

or if you wanted all characters in inpt to be in letters (so only a, b, c or d can be used, use the inverse:

if set(inpt) <= set(letters):
    # *all* of inpt is in letters

Demo:

>>> letters = ['a', 'b', 'c', 'd']
>>> inpt = 'the quick brown fox jumped over the lazy dog'
>>> set(letters) & set(inpt)
{'d', 'c', 'a', 'b'}
>>> set(letters) <= set(inpt)
True
>>> inpt = 'no letters in use!'
>>> set(letters) & set(inpt)
set()
>>> set(letters) <= set(inpt)
False
>>> inpt = 'abbacac'
>>> set(inpt) <= set(letters)
True

where an empty set() is considered false when used in an if statement, a non-empty set() is considered true.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • doesn't work. maybe because inpt is a string and letters is a list – Lara Jan 30 '15 at 13:54
  • @Lara: define *doesn't work*. What did you enter at the input prompt? `set(letters)` creates a set of the letters, `set(inpt)` does the same for a string. – Martijn Pieters Jan 30 '15 at 13:55
  • @MartijnPieters: It looks like Lara wants `set(inpt) <= set(letters)`, not the other way around. – PM 2Ring Jan 30 '15 at 14:21
  • @PM2Ring: right, which is exactly the opposite from what [was stated by the OP](http://stackoverflow.com/questions/28237231/how-to-check-if-entered-letters-are-in-a-list/28237261?noredirect=1#comment44835802_28237231). Or at best they used very imprecise terminology (with *letters* referring to the characters in `inpt`). – Martijn Pieters Jan 30 '15 at 14:26
  • Perhaps. This grouping: _How can I check if (the letters of a word that the user entered) are all in a list?_ seems pretty natural to me (I'm a native English speaker). And the _in a list_ seems most likely to refer to the constant list. – PM 2Ring Jan 30 '15 at 14:37
  • @PM2Ring: sure, on re-reading I can see how it was meant to be read; but reusing `letters` plus an overuse of type conversions didn't make it obvious on first reading. – Martijn Pieters Jan 30 '15 at 14:47
4

For example like this?

letters = set(["a","b","c","d"])
inpt = raw_input()
if set(inpt).issubset(letters):
    print "letters are in list"
else:
    print "letters are not in list"

cf. Python - verifying if one list is a subset of the other

Community
  • 1
  • 1
zormit
  • 533
  • 4
  • 16
  • You already converted the `letters` list into a set, so you don't need to do that again inside the `.issubset()` call. Similarly, the `str(raw_input())` is redundant since `raw_input()` returns a string (but I guess you just copied that from the OP). – PM 2Ring Jan 30 '15 at 14:08
  • @Lara: this is the *inverse* of what you said it should do. Use `set(inpt) <= set(letters)` rather than `set(letters) <= set(inpt)`. You wanted all letters in input to be in `letters`, not all `letters` to be in `inpt`. Or did you use *letters* to mean the characters in `inpt`? In which case you used very confusing wording. – Martijn Pieters Jan 30 '15 at 14:24
  • @zormit: your code is rather redundant. You can use `set(inpt).issubset(letters)` here, no need for the extra `list()` and `set()` calls. – Martijn Pieters Jan 30 '15 at 14:27
  • @MartijnPieters I wanted to _check_ whether the letters from "inpt" are in the list "letters" – Lara Jan 30 '15 at 14:29
  • @Lara: yes, that much is now clear, but [your earlier comment](http://stackoverflow.com/questions/28237231/how-to-check-if-entered-letters-are-in-a-list/28237348?noredirect=1#comment44835802_28237231) was not clear and used confusing terminology. – Martijn Pieters Jan 30 '15 at 14:35
  • @MartijnPieters: yeah, thx, you're right. I removed the redundancy. – zormit Jan 30 '15 at 16:11