Python Forum
using element on a list as condition statement
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
using element on a list as condition statement
#10
Your example is not correct.
When using set in the first approach (using in and set) you measure also the conversion to set

the correct comparison between the two approaches would be:

from timeit import timeit
print(timeit('target in spam', setup='spam = set(range(11, 1000)); target=11'))
print(timeit('any(target == item for item in spam)', setup='spam = set(range(11, 1000)); target = 11'))
Output:
0.02962722000006579 0.45690207600000576
from timeit import timeit
print(timeit('target in spam', setup='spam = set(range(11, 1000)); target=999'))
print(timeit('any(target == item for item in spam)', setup='spam = set(range(11, 1000)); target = 999'))
Output:
0.046424419999993916 56.531857142000035
same result, with using list in both cases:

from timeit import timeit
print(timeit('target in spam', setup='spam = list(range(11, 1000)); target=11'))
print(timeit('any(target == item for item in spam)', setup='spam = list(range(11, 1000)); target = 11'))
Output:
0.041607157000044026 0.4492151309998462
from timeit import timeit
print(timeit('target in spam', setup='spam = list(range(11, 1000)); target=999'))
print(timeit('any(target == item for item in spam)', setup='spam = list(range(11, 1000)); target = 999'))
Output:
11.636881140000014 49.844653193999875
just the set conversion, i.e. how much you "add" in your set examples:
from timeit import timeit
print(timeit('set(spam)', setup='spam = list(range(11, 1000))'))
Output:
13.877401454999927
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
RE: using element on a list as condition statement - by buran - May-21-2020, 04:00 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  element in list detection problem jacksfrustration 5 567 Apr-11-2024, 05:44 PM
Last Post: deanhystad
  unable to remove all elements from list based on a condition sg_python 3 559 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  list in dicitonary element problem jacksfrustration 3 826 Oct-14-2023, 03:37 PM
Last Post: deanhystad
  Find (each) element from a list in a file tester_V 3 1,315 Nov-15-2022, 08:40 PM
Last Post: tester_V
  Сheck if an element from a list is in another list that contains a namedtuple elnk 8 1,982 Oct-26-2022, 04:03 PM
Last Post: deanhystad
  Membership test for an element in a list that is a dict value for a particular key? Mark17 2 1,281 Jul-01-2022, 10:52 PM
Last Post: Pedroski55
  List Creation and Position of Continue Statement In Regular Expression Code new_coder_231013 3 1,758 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  How to find the second lowest element in the list? Anonymous 3 2,120 May-31-2022, 01:58 PM
Last Post: Larz60+
  check if element is in a list in a dictionary value ambrozote 4 2,100 May-11-2022, 06:05 PM
Last Post: deanhystad
  select Eof extension files based on text list of filenames with if condition RolanRoll 1 1,588 Apr-04-2022, 09:29 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020