[Tutor] Iterate through dictionary values and remove item

Kent Johnson kent37 at tds.net
Thu May 15 19:26:27 CEST 2008


On Thu, May 15, 2008 at 12:58 PM, GTXY20 <gtxy20 at gmail.com> wrote:
> I suspect that I need
> to get a better handle on the difference between items() and iteritems() and
> what situations would call for them respectively.

items() returns a list, iteritems() returns an iterator. If you don't
actually need an explicit list, iteritems() saves the cost of creating
it.

> Having said that, Kent I am not 100 percent sure of what you menat when you
> mention a two-level dict. Can you give me a very brief example?

Your dict d would look like this:
d={
    1: {23A:[a,b,c,d],  24A:[b,c,d]},
    2: {23A:[a,b], 24A:[a,b,c,d]}
  }

The top level would just use the first element of the current key; the
value would be another dict whose keys are the second element of the
current key. This makes it easy to find all the values whose first key
element is '1', for example. (You could also have the second element
be a list of tuples, that might be better if you just need to iterate
over it.)

Kent


More information about the Tutor mailing list