searching a list of dictionaries for an element in a list.

Dan dan at cellectivity.com
Wed Aug 10 06:18:38 EDT 2005


> I want to search list1, and the result should be all dictionaries where
> primarycolor is in input. I can do this using a double for-loop, but is
> there a more efficent way?

Of course.    :-)

L = [dict for dict in list1 if dict['primarycolor'] in input]

In older versions of Python, we would use the "filter" function, which
accomplishes the same thing.

-- 
  [Mozilla] Firefox is better than Explorer by leaps and bounds. I
  don't miss Explorer one iota. Give Firefox a day's worth of Web
  surfing, and you won't either.
      - Forbes (short link: http://tinyurl.com/56j8m )





More information about the Python-list mailing list