473,399 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

the most efficient method of adding elements to the list

alf
Hi,

Would it be .append()? Does it reallocate te list with each apend?

l=[]
for i in xrange(n):
l.append(i)
Thx, A.
Jun 6 '06 #1
4 3787
> Hi,

Would it be .append()? Does it reallocate te list with each apend? Yes it does.
If order of the new element being added doesnt matter you can use append.
If it does, you can use insert().
l=[]
for i in xrange(n):
l.append(i)
Thx, A.
--
http://mail.python.org/mailman/listinfo/python-list


Jun 6 '06 #2
alf wrote:
Would it be .append()? Does it reallocate te list with each apend?

l=[]
for i in xrange(n):
l.append(i)


No, it doesn't. It expands the capacity of the list if necessary.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
It isn't important to come out on top, what matters is to be the one
who comes out alive. -- Bertolt Brecht, 1898-1956
Jun 6 '06 #3
alf wrote:
Would it be .append()? Does it reallocate te list with each apend?


No append does NOT reallocate for every call. Whenever a reallocation
happens, the newsize is proportional to the older size. So you should
essentially get amortized constant time for every append call.

If you want to add a bunch of items in one call.. you should use the
'extend' method.

Regards
Sreeram
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2.2 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFEhQlLrgn0plK5qqURApHbAKC1A2xYXdgyj9+4a4Nkg7 9+rMdfrwCgi4xP
4VMZi08TTCKblHBxMssZEYA=
=DdkB
-----END PGP SIGNATURE-----

Jun 6 '06 #4
alf wrote:
Hi,

Would it be .append()? Does it reallocate te list with each apend?

l=[]
for i in xrange(n):
l.append(i)


<dumb>
FWIW, you'd have the same result with:
l = range(n)
</dumb>
More seriously (and in addition to other anwsers): you can also
construct a list in one path:

l = [i for i in xrange(n)]

or if you want operations and conditionals :

l = [trasform(i) for i in xrange(n) if match_some_cond(i)]

HTH
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
Jun 6 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Narendra C. Tulpule | last post by:
Hi, if you know the Python internals, here is a newbie question for you. If I have a list with 100 elements, each element being a long string, is it more efficient to maintain it as a dictionary...
3
by: Carlos Ribeiro | last post by:
As a side track of my latest investigations, I began to rely heavily on generators for some stuff where I would previsouly use a more conventional approach. Whenever I need to process a list, I'm...
34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
3
by: Daniel Weinand | last post by:
hello ng, i have a problem and a imho an insufficient method of solution. strings should be sorted by specific text pattern and dispayed in groups. the strings are stored in a db and have the...
11
by: hoopsho | last post by:
Hi Everyone, I am trying to write a program that does a few things very fast and with efficient use of memory... a) I need to parse a space-delimited file that is really large, upwards fo a...
2
by: David Pratt | last post by:
Hi. I like working with lists of dictionaries since order is preserved in a list when I want order and the dictionaries make it explicit what I have got inside them. I find this combination very...
22
by: Curious | last post by:
Hi, I am searching for a data structure that stores key-value pairs in it. This data structure is to hold large amounts of key-value pairs, and so needs to be efficient both in insertion and...
15
by: Oswald Kluge | last post by:
Dear Reader, I'm trying to implement the following short algorithm: Given a number n, remove all the multiples of n from the list of non-negative numbers from 1 through a limit k. My...
2
by: =?ISO-8859-1?Q?Marco_K=F6rner?= | last post by:
Hello, I'm working on mapping the car's environment by updating an occupancy grid. An occupancy grid dicretizes the 3D space in small grid elements (voxels). A grid element contains informations...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.