Comments (6)
Yes, you are right, we need an additional of the pros and cons of lists vs. keys. I will write it within the next week. Would be perfect if you could be available as a reviewer :)
By the way: I just tried to reproduce your numbers with a slightly different testcase:
def f_a():
my_list = list()
for i in range(500):
my_list.add(i)
bool(3 in my_list)
def f_b():
my_set = set()
for i in range(500):
my_set.add(i)
bool(3 in my_set)
%timeit f_a()
# 10000 loops, best of 3: 27.6 µs per loop
%timeit f_b()
# 10000 loops, best of 3: 33 µs per loop
This time the difference in execution speed is not that big...
from python-anti-patterns.
That makes sense though, creating a set by appending is still O(n log n)
, whereas creating a list by appending is (worst case) O(n**2)
(python over allocates so to avoid doing a fully copy every time a new element is appended so it isn't as bad as it could be).
Yes, ping me in the PR.
[edit because I am wrong]
from python-anti-patterns.
Python sets are implemented with hash tables, so their creation is still O(n). Although there's more constant-time overhead for sets vs. lists - as well as for adding/appending vs. pre-allocating - it's all asymptotically O(n).
And the timing examples are combining the creation time with just 1 membership test. Naturally if you already have a list, and are only testing membership once, then there's no point in converting to a set.
But I agree there's a general anti-pattern here; it just needs more context, such as repeated membership testing in a loop. In mentoring Pythoneers, I have definitely seen the pattern of over list-ifying, instead of using the right data structure. It seems to stem from not trusting duck-typing and iteration as a protocol.
from python-anti-patterns.
Yeah, I am wrong. Should not post comments before coffee.
A bit more context was all I was asking for.
from python-anti-patterns.
Ok, we will provide more context in the article.
It would be great to have some links concerning the time complexities. I only know https://wiki.python.org/moin/TimeComplexity. But this page does not contain any informations about the time needed for creation of list
/set
. Do you know some good links to include in the article?
from python-anti-patterns.
I found this issue having just read and tested this anti-pattern, so I agree some further clarification would be good. Perhaps simply changing the title to 'repeatedly using key in list', with an explanation of the complexities and practicalities below (O(n) set creation + O(1) checks vs O(n) checks)?
from python-anti-patterns.
Related Issues (20)
- Converting values to lists using list comprehensions instead of list() HOT 7
- PDF link from previous issue not working HOT 2
- Is there some process for translation?
- Calling LBYL an anti-pattern is not exactly correct
- Logging anti-patterns
- Method could be a function HOT 2
- Headline "Test for object identity should be is" doesn't match content
- Using map() or filter() where list comprehension is possible HOT 1
- The little book of python anti-pattern HOT 1
- Add a reference to flake8-builtins
- Perimeter != Circumference
- Not using with to open files - what about pathlib's read_text HOT 2
- Clarify why comparing things to none is discouraged HOT 1
- nvm HOT 1
- Drop the section for Django 1.8
- NullBooleanField is deprecated HOT 2
- Question Regarding Global Statements
- Use f-string when formatting strings
- "Using key in list to check if key is contained in list" -- Nitpick HOT 2
- One of the examples uses the 'list' anti-pattern
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from python-anti-patterns.