0

I am given:

list = [{'a','b'},{'c','d'}]

and I want to know if 'a' is in the list.

should I first unpack the list as here to new_list and the use 'a' in new_list

Or is there a shorter way (without importing modules)

gbox
  • 809
  • 1
  • 8
  • 22

3 Answers3

3

use any

spam = [{'a','b'},{'c','d'}]
eggs = [{'d','b'},{'c','d'}]

print(any('a' in my_set for my_set in spam))
print(any('a' in my_set for my_set in eggs))

Output

True
False
>>>
buran
  • 13,682
  • 10
  • 36
  • 61
1

This is one approach using any.

Ex:

l = [{'a','b'},{'c','d'}]
print( any(map(lambda x: "a" in x, l)) )

Output:

True
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

Hope it's solve it. I write it on a function to use "return" keyword

def a_is_there():

    lists = [{'a','b'},{'c','d'}]
    for list in lists:
        if "a" in list:
            print("True")
            return True
    print("False")
    return False


a_is_there()

Thanks