Execution speed question

S

Suresh Pillai

Since I am doing A LOT of loops over the nodes and the number of nodes
is also huge, my concern using sets is that in order to iterate over the
set in each step of my simulation, the set items need to be converted to
a list every time. So while removal from a set is much cheaper than say
from a list, what about this conversion overhead in order to iterate
over the items.

I could of course use the old trick of using a dictionary with 'None'
values and then using iterkeys(). But I thought sets were supposed to
replace this. So maybe I should be asking a more basic question: is
there any way to iterate over the items in a set other than converting to
a list or using the pop() method.
 
S

Sion Arrowsmith

Suresh Pillai said:
[ ... ] is
there any way to iterate over the items in a set other than converting to
a list or using the pop() method.

Er, how about directly iterating over the set?
 
S

Steven D'Aprano

I could of course use the old trick of using a dictionary with 'None'
values and then using iterkeys(). But I thought sets were supposed to
replace this. So maybe I should be asking a more basic question: is
there any way to iterate over the items in a set other than converting
to a list or using the pop() method.

Yes, just do it.
.... print i
....
1
2
3
 
S

Suresh Pillai

I could of course use the old trick of using a dictionary with 'None'
values and then using iterkeys(). But I thought sets were supposed to
replace this. So maybe I should be asking a more basic question: is
there any way to iterate over the items in a set other than converting
to a list or using the pop() method.

Okay, please consider this my one absolutely stupid post for the year.
I'd like to pretend it never happened but unfortunately the web doesn't
allow that. Having never used sets, I unfort read something that lead to
it, but ...
 
S

Suresh Pillai

Okay, please consider this my one absolutely stupid post for the year.
I'd like to pretend it never happened but unfortunately the web doesn't
allow that. Having never used sets, I unfort read something that lead
to it, but ...

Okay, got some sleep and what I meant to ask, although equally basic, but
not silly:

For sets, I presume they are built on top of or like dicts, and there is
nothing crazy in the low level implementation so that I can be guaranteed
that if I don't alter the set, then the order, although arbitrary, will
be maintained in successive iterations over the contents?
 
D

Diez B. Roggisch

Suresh said:
Okay, got some sleep and what I meant to ask, although equally basic, but
not silly:

For sets, I presume they are built on top of or like dicts, and there is
nothing crazy in the low level implementation so that I can be guaranteed
that if I don't alter the set, then the order, although arbitrary, will
be maintained in successive iterations over the contents?

It is currently that way, but relying on it is certainly to be considered an
implementation detail that might disappear without warning.


Diez
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,690
Latest member
MacGyver

Latest Threads

Top