Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think:</p> <pre><code>#!/bin/python bar in dict(Foo) </code></pre> <p>Is what you are thinking of. When trying to see if a certain key exists within a dictionary in python (python's version of a hash table) there are two ways to check. First is the <strong><code>has_key()</code></strong> method attached to the dictionary and second is the example given above. It will return a boolean value.</p> <p>That should answer your question.</p> <p>And now a little off topic to tie this in to the <em>list comprehension</em> answer previously given (for a bit more clarity). <em>List Comprehensions</em> construct a list from a basic <em>for loop</em> with modifiers. As an example (to clarify slightly), a way to use the <code>in dict</code> language construct in a <em>list comprehension</em>:</p> <p>Say you have a two dimensional dictionary <strong><code>foo</code></strong> and you only want the second dimension dictionaries which contain the key <strong><code>bar</code></strong>. A relatively straightforward way to do so would be to use a <em>list comprehension</em> with a conditional as follows:</p> <pre><code>#!/bin/python baz = dict([(key, value) for key, value in foo if bar in value]) </code></pre> <p>Note the <strong><code>if bar in value</code></strong> at the end of the statement**, this is a modifying clause which tells the <em>list comprehension</em> to only keep those key-value pairs which meet the conditional.** In this case <strong><code>baz</code></strong> is a new dictionary which contains only the dictionaries from foo which contain bar (Hopefully I didn't miss anything in that code example... you may have to take a look at the list comprehension documentation found in <a href="http://docs.python.org/tut/node7.html#SECTION007140000000000000000" rel="nofollow noreferrer">docs.python.org tutorials</a> and at <a href="http://www.secnetix.de/olli/Python/list_comprehensions.hawk" rel="nofollow noreferrer">secnetix.de</a>, both sites are good references if you have questions in the future.).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload