Verify returned list with a list in python? -
i have function returns me single list. i'm trying take returned , find if exists in list have. missing 'for' loop iterate through cat_colors list? or 'color' returned in way can't match in list? trying understand , if have better solution?
for cat in cat_names: color = get_color(cat) print cat_colors print color if color not in cat_colors: print "fail" else: print "pass" output print:
['brown', 'grey', 'white', 'black', 'red', 'orange', 'green', 'blue'] ['brown'] fail ['brown', 'grey', 'white', 'black', 'red', 'orange', 'green', 'blue'] ['grey'] fail ...
['brown'] isn't in ['brown', 'grey', 'white', 'black', 'red', 'orange', 'green', 'blue']. string 'brown' is.
you have 2 options here. if know color one-element list, can do:
if color[0] in cat_colors: ... if not, do:
if any(c in cat_colors c in color): ... # or all(c in cat_colors c in color) based on needs
Comments
Post a Comment