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

operation complexities of lists and dictionaries


hi,
i've just a simple question:
what are the time complexities of inserting / removing / checking if an
element is present in 1) a list and 2) a dictionary?
does anybody know?
thanks

Mar 29 '06 #1
4 981
Hi

Use the "timeit" module, like so:
from timeit import Timer
t = Timer('[i for i in range(10000)]') # The string is code to execute (for timing)
print t.timeit(100) # execute it 100 times and print the result

0.222389936447

I would appreciate it if you could present your results in this thread.
I have also been wondering about timings for simple operations.

Thanks
Caleb

Mar 29 '06 #2
On Wed, 29 Mar 2006 09:38:05 -0800, xa******@gmail.com wrote:

hi,
i've just a simple question:
what are the time complexities of inserting / removing / checking if an
element is present in 1) a list and 2) a dictionary?
does anybody know?
thanks


No no no, that's not the way to ask the question, you're hardly likely to
get answers that way.

What you do is, you write in and say "I need a more efficient way of
adding data to a list, because adding data to a list is O(n**2) and that's
too slow. Also, how do I implement hash tables in Python, because I need
O(1) searching?"

That way you'll have dozens, maybe hundreds of replies from people
explaining that adding data to Python lists is O(log n) amortized, and
that dictionaries are hash tables.

*wink*

--
Steven.

Mar 29 '06 #3
"xa******@gmail.com" <xa******@gmail.com> writes:
what are the time complexities of inserting / removing / checking if an
element is present in 1) a list and 2) a dictionary?
Partly dependent on the implementation, of which there are several for
Python (CPython, Jython, PyPy, and others). Which one are you most
interested in?
does anybody know?


The 'timeit' module can help you discover the answers for yourself, on
your current implementation.

--
\ "You could augment an earwig to the point where it understood |
`\ nuclear physics, but it would still be a very stupid thing to |
_o__) do!" -- The Doctor, _The Two Doctors_ |
Ben Finney

Mar 29 '06 #4
"xa******@gmail.com" <xa******@gmail.com> writes:
what are the time complexities of inserting / removing / checking if an
element is present in 1) a list and 2) a dictionary?
does anybody know?


I assume in the list case, the element you want to operate on is in
the middle of the list. In CPython, those are linear time operations.
If you just want to append to the end or remove from the end, those
are approximately constant time. For dicts, all those operations are
approximately constant time in CPython. Approximately means
occasionally something happens that makes it slower, but it's constant
time on average.

The Python docs don't specify this anywhere but it's so fundamental to
how people expect Python to work that it's not likely to be much worse
in any other implementation.
Mar 29 '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...
5
by: Paul Moore | last post by:
I can't find anything which spells this out in the manuals. I guess that, at some level, the answer is "a single bytecode operation", but I'm not sure that explains it for me. This thought was...
2
by: Stewart Midwinter | last post by:
I would like to link the contents of three OptionMenu lists. When I select an item from the first list (call it continents), the contents of the 2nd list (call it countries) would update. And in...
1
by: Gabriel B. | last post by:
I just sent an email asking for hints on how to import data into a python program As i said earlier i'm really new to python and besides being confortable with the syntax, i'm not sure if i'm on...
10
by: Philippe C. Martin | last post by:
Hi, I'm looking for an easy algorithm - maybe Python can help: I start with X lists which intial sort is based on list #1. I want to reverse sort list #1 and have all other lists sorted...
9
by: Dave H | last post by:
Hello, I have a query regarding definition lists. Is it good practice semantically to use the dt and dd elements to mark up questions and answers in a frequently asked questions list, or FAQ? ...
9
by: SMB | last post by:
I have two lists of data like the following: LIST1 , ] LIST2 , 'label': 'First Name', 'width': 0L, 'separator': ',', 'height': 0L, 'type': 2L, 'order': 1L}, {'code': 14L, 'name': 'Last...
4
by: bharath539 | last post by:
how to write programmes which minimizes time and space complexities let suppose for linked lists related programmes
5
by: Ladislav Andel | last post by:
Hi, I have a list of dictionaries. e.g. how could I make a new list of dictionaries which would look like: , 'service_domain': 'dp0.example.com'}, {'transports': , 'service_domain':...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...

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.