Grant Edwards wrote:
Here's another choice, that's sometimes handy:
d = {1:'one',2:'two',3:'three'}
for k,v in d.items():
print k,v
1 one
2 two
3 three
I wouldn't recommend this for large dictionaries.
Yes, for large dictionaries, you should use:
for k, v in d.iteritems():
print k,
[EMAIL PROTECTED] wrote:
1) Is there any advantage to use the
y = a.keys()
for z in y:
looping technique rather than the
for x in a:
looping technique?
2) What are the tradeoffs for using each of the techniques?
Calling dict.keys creates a list in memory of the keys to the dict.
Using the dict dir
On 2005-02-17, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> 1) Is there any advantage to use the
>
> y = a.keys()
> for z in y:
>
> looping technique rather than the
>
> for x in a:
>
> looping technique?
Not really.
> 2) What are the tradeoffs for using each of the techniques?
"for x in a" ca
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1
> "Miranda" == mirandacascade <[EMAIL PROTECTED]> writes:
Miranda> 1) Is there any advantage to use the
Miranda> y = a.keys()
Miranda> for z in y:
While you're at it, you could save y altogether and just use
for z in a.keys():
...
Miranda>
I am attempting to understand the difference between two techniques
that use a for/in loop to iterate through entries in a dictionary
object. Copy/paste of interactive window illustrates.
>>> a = {}
>>> a.update({1:'a'})
>>> a.update({2:'b'})
>>> a
{1: 'a', 2: 'b'}
>>> for x in a:
... print x