Preallocate? -- potentially brain dead question about performance

J

Jan Danielsson

Hello all,

I have a list which contains a bunch of tuples:

mylist = [ ('1', 'Foobar'), ('32', 'Baz'), ('4', 'Snorklings') ]

(The list can potentially be shorter, or much longer). Now I want to
take the first element in each tuple and store it in a list (to use in a
Set later on).

Which would You prefer of the following:

newlist = [ ]
for e in mylist:
newlist.append(int(e[0]))

..or..

newlist = [ None ] * len(mylist)
for i in range(len(mylist)):
newlist.append(int(e[0]))


To me, the second one is more appealing, when I think in terms of
risk of memory fragmentation, etc. But since Python is such a high level
language, I'm not sure my traditional reasoning applies.
 
B

bearophileHUGS

Jan Danielsson:
newlist = [ None ] * len(mylist)
for i in range(len(mylist)):
newlist.append(int(e[0]))

Note that this appends after the Nones, not over them. And note that
here the name 'e' is undefined.

The most used idiom for that is:

newlist = [int(e[0]) for e in mylist]

Or better lazily if you want to create a set, avoiding the list
creation:

newlist = set(int(e[0]) for e in mylist)

Bye,
bearophile
 
J

Jan Danielsson

newlist = [ None ] * len(mylist)
for i in range(len(mylist)):
newlist.append(int(e[0]))

Note that this appends after the Nones, not over them. And note that
here the name 'e' is undefined.

What an idiot I am.. Yes, I know that.. And I actually *had* working
code laying around, but rather than opening it and copy-and-pasting, I
simply rewrote it. I obviously meant:

newlist = int(e[0])
The most used idiom for that is:

newlist = [int(e[0]) for e in mylist]
Or better lazily if you want to create a set, avoiding the list
creation:

newlist = set(int(e[0]) for e in mylist)

...completely avoiding the design issue I raised altogether. Thanks!
Exactly what I was hoping for! :)
 
B

bearophileHUGS

Jan Danielsson:
...completely avoiding the design issue I raised altogether. Thanks!
Exactly what I was hoping for! :)

Another common way to do it, it may be a little slower because that
n,txt is a tuple:

items = set(int(n) for n,txt in mylist)

Bye,
bearophile
 

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

Similar Threads

Understanding While Loop Execution 3
UnicodeEncodeError in Windows 2
generate and send mail with python: tutorial 8
ANN main-4.4.0 0
[ANN] main-3.0.1 0
anybody help me 1
7.0 wishlist? 321
Download counter 0

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top