5

I have a list called bag. I want to be able to check if only a particular item is in it.

bag = ["drink"]
if only "drink" in bag:
    print 'There is only a drink in the bag'
else:
    print 'There is something else other than a drink in the bag'

Of course, where I put 'only' in the code there, it is wrong. Is there any simple replacement of that? I have tried a few similar words.

Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
reemer9997
  • 341
  • 1
  • 4
  • 7
  • if len(bag) == 1 and item in bag: – Haifeng Zhang Mar 24 '16 at 15:28
  • Sorry I'm not clear here, but do you mean that the list has only one item and it is "drink", or just that "drink" is in the list? In other words, would ["drink", "candy bar"] pass your if only test? – PTBNL Mar 24 '16 at 15:30
  • You don't try "a few similar words" to do something in a programming language, but what you do is learn the basics of the language first, and then use your knowledge to do stuff. That's the natural process of learning. – Sнаđошƒаӽ Mar 24 '16 at 15:37
  • 1
    @Sнаđошƒаӽ monte-carlo search for keywords that do what you want, that isn't how you program? You're missing out – en_Knight Mar 24 '16 at 15:41
  • @en_Knight Sorry but I failed to understand what you mean. – Sнаđошƒаӽ Mar 24 '16 at 15:42
  • 1
    Possible dup. http://stackoverflow.com/questions/405516/if-all-in-list-something and Shadowfax just a joke (randomly guess words until sometihng works) – en_Knight Mar 24 '16 at 15:44
  • @en_Knight I thought so to myself, I was just being cautious :D – Sнаđошƒаӽ Mar 24 '16 at 15:47

3 Answers3

14

Use the builtin all() function.

if bag and all(elem == "drink" for elem in bag):
    print("Only 'drink' is in the bag")

The all() function is as follows:

def all(iterable):
    for element in iterable:
        if not element:
            return False
    return True

For this reason, an empty list will return True. Since there are no elements, it will skip the loop entirely and return True. Because this is the case, you must add an explicit and len(bag) or and bag to ensure that the bag is not empty (() and [] are false-like).

Also, you could use a set.

if set(bag) == {['drink']}:
    print("Only 'drink' is in the bag")

Or, similarly:

if len(set(bag)) == 1 and 'drink' in bag:
    print("Only 'drink' is in the bag")

All of these will work with 0 or more elements in the list.

Goodies
  • 4,439
  • 3
  • 31
  • 57
1

You could directly check for equality with a list that only contains this item:

if bag == ["drink"]:
    print 'There is only a drink in the bag'
else:
    print 'There is something else other than a drink in the bag'

Alternatively if you want to check whether the lists contains any number greater than zero of the same item "drink", you can count them and compare with the list length:

if bag.count("drink") == len(bag) > 0:
    print 'There are only drinks in the bag'
else:
    print 'There is something else other than a drink in the bag'
Byte Commander
  • 6,506
  • 6
  • 44
  • 71
0

You can check length of list

if len(bag) == 1 and "drink" in bag:
    #do your operation.
Nilesh
  • 20,521
  • 16
  • 92
  • 148