473,421 Members | 1,491 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,421 software developers and data experts.

list implementation

sj
I believe the type "list" is implemented as an array of pointers.
Thus, random access is an O(1) operation while insertion/deletion is an
O(n) operation. That said, I have the following questions:

1. Am I correct in saying the above?

2. Implementing list as an array is part of language specification or
implementation-dependent?

3. What if I actually need a doubly-linked list for constant-time
insertion/deletion? Is there a built-in type or a standard class?

Thanks.

Jul 21 '05 #1
4 9474

"sj" <se*******@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
I believe the type "list" is implemented as an array of pointers.
A Python list is sematically/behaviorally defined as a mutable extensible
sequence of references to Python objects. For the CPython reference
implementation, the references are arrays of C pointers. Since, on
balance, this works well, I presume the other four active computer language
implementations do something equivalent. (How we humans execute Python
code is a different question!)
Thus, random access is an O(1) operation
Yes
while insertion/deletion is an O(n) operation.
At the front, yes. (But modern CPUs with a one-instruction block mem move
make the hidden multiplier relatively small.) At the end of the list, no;
it is O(1). Making front insertion/deletion (but not in the middle) also
O(1) has been considered but so far rejected. (For apps that need the
symmetry, there is collections.deque.)
2. Implementing list as an array is part of language specification or
implementation-dependent?
While I believe I have answered this, I recommend reading the relevant
parts of the language and library manuals (see chapter 2 of the latter).
3. What if I actually need a doubly-linked list for constant-time
insertion/deletion? Is there a built-in type or a standard class?


I believe it is roll-your-own to your specific needs. Of course, scanning
thru such a list is still O(n).

Terry J. Reedy

Jul 21 '05 #2
[sj]
I believe the type "list" is implemented as an array of pointers.
Yes.

Thus, random access is an O(1) operation while insertion/deletion is an
O(n) operation.
Yes.

2. Implementing list as an array is part of language specification or
implementation-dependent?
Implementation dependent but likely to be an array of pointers.

3. What if I actually need a doubly-linked list for constant-time
insertion/deletion? Is there a built-in type or a standard class?


Yes. Use collections.deque().

http://docs.python.org/tut/node13.ht...00000000000000
http://docs.python.org/lib/module-collections.html
Raymond

Jul 21 '05 #3
Raymond Hettinger <py****@rcn.com> wrote:
[sj]
Thus, random access is an O(1) operation while insertion/deletion is an
O(n) operation.
Yes.


Unfortunately no. Check Terry Reeds answer. Random access is O(1),
insertion/deletion to front is O(n), and i/d to back is O(1). The back
i/d operation has amortized O(1) cost.

--
Heikki Orsila Barbie's law:
he***********@iki.fi "Math is hard, let's go shopping!"
http://www.iki.fi/shd
Jul 21 '05 #4
> > [sj]
Thus, random access is an O(1) operation while insertion/deletion is an
O(n) operation.

[Raymond Hettinger] Yes.

[Heikki Orsila aka host.invalid] Unfortunately no. Check Terry Reeds answer. Random access is O(1),
insertion/deletion to front is O(n), and i/d to back is O(1). The back
i/d operation has amortized O(1) cost.


Duh. Excellent misreading of a simple answer to the OP's plain
question. It is clear from his post that he already had a pretty
accurate guess about how lists were implemented and the consequent
performance implications.

What was needed was a confirmation of his guess and a pointer to
collections.deque() for O(1) insertion/deletion on the ends. For O(1)
length changing ops in the middle, his suggestion of a linked list was
not off base. IOW, a simple yes would have served.
Raymond
P.S. Non-standard abbreviations like, i/d, are rarely helpful to your
readers.

Jul 23 '05 #5

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

Similar topics

46
by: J.R. | last post by:
Hi folks, The python can only support passing value in function call (right?), I'm wondering how to effectively pass a large parameter, such as a large list or dictionary? It could achieved...
9
by: kazio | last post by:
Hello, So, I need to have double linked, circular list (last element point to the first one and first one points to the last one). I thought maybe I could use list container from STL, but...
12
by: Brett L. Moore | last post by:
Hi, I have had trouble determining whether the STL list.size() operation is O(1) or O(n). I know the list is a doubly-linked list, so if the size() operation begins at the head, then counts to...
5
by: Darryl B | last post by:
I can not get anywhere on this project I'm tryin to do. I'm not expecting any major help with this but any would be appreciated. The assignment is attached. The problem I'm having is trying to set...
15
by: sandwich_eater | last post by:
I want to know how to set an std::list iterator variable to make it null or nil. If this is not possible what is the value of an uninitialised std::list iterator and is it ok to assign this value...
35
by: Thierry Loiseau | last post by:
Hello all, and Happy end year 2005 ! Well, I would like to obtain a list of all JavaScript var statement, With "for...in" perharps ? That is bellow my recent test here, but the problem is...
4
by: Rene | last post by:
According to the documentation, the List<T> type explicitly implements the non generic IList interface. The problem is that no matter how hard I look, I am not able to find this implemetion on...
6
by: Heiko Wundram | last post by:
Hi all! The following PEP tries to make the case for a slight unification of for statement and list comprehension syntax. Comments appreciated, including on the sample implementation. ===...
6
by: Amit Bhatia | last post by:
Hi, I am not sure if this belongs to this group. Anyway, my question is as follows: I have a list (STL list) whose elements are pairs of integers (STL pairs, say objects of class T). When I create...
9
by: william | last post by:
When implementing Linked list, stack, or trees, we always use pointers to 'link' the nodes. And every node is always defined as: struct node { type data; //data this node contains ... node *...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.