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

Set builtin lookups

I have a question about the internal representation of sets in Python.
If I write some code like

if x in some_list:
do_something()

the lookup for the in statement is O(n), where n is the number of
elements in the list. Is this also true if I am using a set or are
sets represented by a hash table?

--
Evan Klitzke <ev**@yelp.com>
Jun 27 '07 #1
3 1046
In <ma**************************************@python.o rg>, Evan Klitzke
wrote:
I have a question about the internal representation of sets in Python.
If I write some code like

if x in some_list:
do_something()

the lookup for the in statement is O(n), where n is the number of
elements in the list. Is this also true if I am using a set or are
sets represented by a hash table?
Sets are implemented as hash tables.

Ciao,
Marc 'BlackJack' Rintsch
Jun 27 '07 #2
Marc 'BlackJack' Rintsch wrote:
In <ma**************************************@python.o rg>, Evan Klitzke
wrote:
>I have a question about the internal representation of sets in Python.
If I write some code like

if x in some_list:
do_something()

the lookup for the in statement is O(n), where n is the number of
elements in the list. Is this also true if I am using a set or are
sets represented by a hash table?

Sets are implemented as hash tables.

Ciao,
Marc 'BlackJack' Rintsch
More importantly, no, neither sets nor dictionaries have O(N) lookup
costs. As an experiment or two can show.

regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
--------------- Asciimercial ------------------
Get on the web: Blog, lens and tag the Internet
Many services currently offer free registration
----------- Thank You for Reading -------------

Jun 27 '07 #3
Steve Holden <st***@holdenweb.comwrote:
Marc 'BlackJack' Rintsch wrote:
In <ma**************************************@python.o rg>, Evan Klitzke
wrote:
I have a question about the internal representation of sets in Python.
If I write some code like

if x in some_list:
do_something()

the lookup for the in statement is O(n), where n is the number of
elements in the list. Is this also true if I am using a set or are
sets represented by a hash table?
Sets are implemented as hash tables.

Ciao,
Marc 'BlackJack' Rintsch

More importantly, no, neither sets nor dictionaries have O(N) lookup
costs. As an experiment or two can show.
The exception would be for objects with a bad or unlucky __hash__ that
happens to return the same value for every object:

class oops(object):
def __hash__(self): return 123456

this one you might expect to produce O(N) behavior:-).
Alex
Jun 29 '07 #4

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

Similar topics

5
by: Blair Hall | last post by:
Can anyone please tell me how to correctly use a built in function when there is a function of the same name in local scope? Here is an example. Suppose the following is in myApply.py: def...
1
by: Stephen Ferg | last post by:
Python has a builtin class for staticmethod. Seems to me that Python should also have a builtin class for abstractmethod. Something like this... ####################################### #...
1
by: barnesc | last post by:
Hi again, Since my linear algebra library appears not to serve any practical need (I found cgkit, and that works better for me), I've gotten bored and went back to one of my other projects:...
1
by: Russell | last post by:
Hi, I've been reading a lot about not using lookups in tables lately. Thus far, what I've been able to understand from my reading is this: - Do not use lookup fields in tables because they...
5
by: AnAnimal | last post by:
We are moving a rather large application to the VB.NET platform. This application has a ton of table lookups. ie. code & description or code, category & description or code, class, category &...
6
by: Anders K. Olsen | last post by:
Hello group I'm trying to list the users and groups who has read access to a file. I use .NET 2.0 and FileInfo.GetAccessControl().GetAccessRules(...) and then loop through the...
6
by: mac | last post by:
Summary: 1. I want to define a column in anMS Access table to be lookups on other tables. 2. The table that is the data source (e.g the "parent" table) has a composite primary key. 3. When...
0
by: nejucomo | last post by:
Hi folks, Quick Synopsis: A test script demonstrates a memory leak when I use pythonic extensions of my builtin types, but if I use the builtin types themselves there is no memory leak. ...
2
by: King Ron | last post by:
Ola all. In responding to a recent post requesting help with a search issue, I recommended using a combo box lookup in the table design. "paii, Ron" (no relation) posted this reply: " There are...
4
by: netnewbie78 | last post by:
Hello All, I don't have a problem (but maybe I will after I explain). I have a question with regards to something I saw in Access 2007. But first, a little backstory: I'm writing a very small...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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...
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:
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...

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.