Test if all values are in an iterable in a pythonic way

blue_zinc :

I am currently doing this:

if x in a and y in a and z in a and q in a and r in a and s in a:
    print b

Is there a more pythonic way to express this if statement?

Bas Swinckels :

Using the all function allows to write this in a nice and compact way:

if all(i in a for i in (x, y, z, q, r, s)):
    print b

This code should do almost exactly the same as your example, even if the objects are not hashable or if the a object has some funny __contains__ method. The all function also has similar short-circuit behavior as the chain of and in the original problem. Collecting all objects to be tested in a tuple (or a list) will guarantee the same order of execution of the tests as in the original problem. If you use a set, the order might be random.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Is there a Pythonic way to extend one dictionary with another (preserving all values)

From Java

Pythonic way to create union of all values contained in multiple lists

From Dev

Pythonic way to setup fixture for test

From Dev

pythonic way of setting values in a dictionary

From Dev

pythonic way of reassigning values to dictionary

From Dev

Replacing values in dataframe in a Pythonic way

From Dev

Pythonic way of writing `for my_var in my_iterable`

From Dev

A pythonic way to mask all values after specific index in a 2D numpy array with 0

From Dev

A pythonic way to code self writing test methods

From Dev

Pythonic way to map enum to api values

From Dev

Pythonic way to populate a dictionary with count of values

From Python

Pythonic way to replace values in a list according to a dictionary

From Dev

How to Invert column values in pandas - pythonic way?

From Dev

Pythonic way to check empty dictionary and empty values

From Dev

Pythonic way to create pairs of values in a column in dataframe

From Dev

Pythonic Way to Compare Values in Two Lists of Dictionaries

From Dev

Pythonic way to calculate the mean and variance of values in Counters

From Dev

Pythonic way to select elements of an array based on values?

From Dev

More pythonic way to change multilevel dictionary values

From Dev

Pythonic way to find if a string contains multiple values?

From Dev

pythonic way for function with loads of parameters and return values

From Dev

pythonic way to reverse a dict where values are lists?

From Dev

pythonic way to simplify if clause with multiple values

From Dev

Pythonic way to shift all keys in dict

From Dev

Pythonic way of comparing all adjacent elements in a list

From Dev

how to create all pairs in a more pythonic way?

From Dev

pythonic way to find all potential longest sequence

From Java

Pythonic way of applying regex to all columns of dataframe

From Dev

Pythonic way to catch all exceptions from a module?

Related Related

  1. 1

    Is there a Pythonic way to extend one dictionary with another (preserving all values)

  2. 2

    Pythonic way to create union of all values contained in multiple lists

  3. 3

    Pythonic way to setup fixture for test

  4. 4

    pythonic way of setting values in a dictionary

  5. 5

    pythonic way of reassigning values to dictionary

  6. 6

    Replacing values in dataframe in a Pythonic way

  7. 7

    Pythonic way of writing `for my_var in my_iterable`

  8. 8

    A pythonic way to mask all values after specific index in a 2D numpy array with 0

  9. 9

    A pythonic way to code self writing test methods

  10. 10

    Pythonic way to map enum to api values

  11. 11

    Pythonic way to populate a dictionary with count of values

  12. 12

    Pythonic way to replace values in a list according to a dictionary

  13. 13

    How to Invert column values in pandas - pythonic way?

  14. 14

    Pythonic way to check empty dictionary and empty values

  15. 15

    Pythonic way to create pairs of values in a column in dataframe

  16. 16

    Pythonic Way to Compare Values in Two Lists of Dictionaries

  17. 17

    Pythonic way to calculate the mean and variance of values in Counters

  18. 18

    Pythonic way to select elements of an array based on values?

  19. 19

    More pythonic way to change multilevel dictionary values

  20. 20

    Pythonic way to find if a string contains multiple values?

  21. 21

    pythonic way for function with loads of parameters and return values

  22. 22

    pythonic way to reverse a dict where values are lists?

  23. 23

    pythonic way to simplify if clause with multiple values

  24. 24

    Pythonic way to shift all keys in dict

  25. 25

    Pythonic way of comparing all adjacent elements in a list

  26. 26

    how to create all pairs in a more pythonic way?

  27. 27

    pythonic way to find all potential longest sequence

  28. 28

    Pythonic way of applying regex to all columns of dataframe

  29. 29

    Pythonic way to catch all exceptions from a module?

HotTag

Archive