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
#1
how to implement this code ?
i want any value in the list1 be compared in variable sum
if has , statements will follow
if no , prints a string as stated below

sum = 33
list1=[11,22,33,44,55] 
if (sum == list1): #  should compare every element on this list
    # some statements
else:
   print("no equal value")
    
Reply
#2
sum = 33
list1=[11,22,33,44,55] 
if sum in list1:
    print(f'{sum} is in list')
else:
   print(f'{sum} is not in list')
Output:
33 is in list
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
My Github
How to post code using bbtags


Reply
#3
(May-19-2020, 09:07 AM)glennford49 Wrote: how to implement this code ?
i want any value in the list1 be compared in variable sum
if has , statements will follow
if no , prints a string as stated below

sum = 33
list1=[11,22,33,44,55] 
if (sum == list1): #  should compare every element on this list
    # some statements
else:
   print("no equal value")
    
Use in, not ==
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#4
(May-19-2020, 09:15 AM)menator01 Wrote:
sum = 33
list1=[11,22,33,44,55] 
if sum in list1:
    print(f'{sum} is in list')
else:
   print(f'{sum} is not in list')
Output:
33 is in list
thanks for the quick response!
works like a charm
+1 buddy
Reply
#5
Don't use sum as variable name, it's a built-in function
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
#6
One way can go through each item in the list with a loop and compare them to your sum, but this is not an efficient way to do. So I guess you should go with the answer of Menator01 with a slight modification. It is more efficient if you convert your list to a set just for comparison purpose. A list can have the same element multiple times, when converting it to a set, it ll only contain element once. Thus the search space has decreased and it 'll be faster.
sum = 33
list1=[11,22,33,44,55] 
if sum in set(list1):
    print(f'{sum} is in list')
else:
    print(f'{sum} is not in list')
Reply
#7
(May-20-2020, 12:12 PM)hussainmujtaba Wrote: It is more efficient if you convert your list to a set just for comparison purpose. A list can have the same element multiple times, when converting it to a set, it ll only contain element once.

I think that using built-in any() is even more efficient approach. Due to short-circuiting nature it will stop if first match encountered. In real life scenarios this means that if there is a match you don't need to go through all items in list or convert whole list into set. One can craft such a code:

>>> target = 33
>>> lst = [11, 22, 33, 44, 55]  
>>> match = ['is not', 'is'][any(target == item for item in lst)]               
>>> f'{target} {match} in the list'                                             
'33 is in the list'                                                             
>>> target = 10                                                                 
>>> match = ['is not', 'is'][any(target == item for item in lst)]               
>>> f'{target} {match} in the list'                                             
'10 is not in the list'    
Instead of ['is not', 'is'] one can use ['not', ''] and put 'is' into string but it would be too cryptic for my taste.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#8
(May-20-2020, 02:40 PM)perfringo Wrote: I think that using built-in any() is even more efficient approach.
nope, it's not

from timeit import timeit

print(timeit("any(11==item for item in [11, 22, 33, 44, 55])"))
print(timeit("11 in [11, 22, 33, 44, 55]"))

print(timeit("any(55==item for item in [11, 22, 33, 44, 55])"))
print(timeit("55 in [11, 22, 33, 44, 55]"))
Output:
0.4314083050001045 0.022730736999847068 0.6241739300000972 0.076374732999966
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
#9
(May-20-2020, 03:20 PM)buran Wrote:
(May-20-2020, 02:40 PM)perfringo Wrote: I think that using built-in any() is even more efficient approach.
nope, it's not

You are right, even though I referred 'even more efficient' compared to creating set() and then making lookup. But still you are right :-).


>>> test = '''\
... lst = [11, 22, 33, 44, 55]
... target = 11
... target in set(lst)
... '''
>>> timeit(stmt=test, number=10000)
0.008103398999082856
>>> test = '''\
... lst = [11, 22, 33, 44, 55]
... target = 11
... any(target == item for item in lst)
... '''
>>> timeit(stmt=test, number=10000)
0.009728211000037845
I am no expert in timing but it got me interested. If target is first in list and list is little bit bigger then:

>>> test = '''\
... lst = list(range(11, 1000))
... target = 11
... target in set(lst)
... '''
>>> timeit(stmt=test, number=10000)
0.2991924960006145
>>> test = '''\
... lst = list(range(11, 1000))
... target = 11
... any(target == item for item in lst)
... '''
>>> timeit(stmt=test, number=10000)
0.16373216899955878
However, if target is more in the middle of list:

>>> test = '''\
... lst = list(range(11, 1000))
... target = 500
... target in set(lst)
... '''
>>> timeit(stmt=test, number=10000)
0.29931609900086187
>>> test = '''\
... lst = list(range(11, 1000))
... target = 500
... any(target == item for item in lst)
... '''
>>> timeit(stmt=test, number=10000)
0.4031077369945706
If using timeit with setup, the results are (target is first item in list):

>>> timeit('target in set(lst)', setup='lst = list(range(11, 1000)); target=11') 
13.232636451997678
>>> timeit('any(target == item for item in lst)', setup='lst = list(range(11, 1000)); target = 11')
0.427400492997549
Target is in the middle:

>>> timeit('target in set(lst)', setup='lst = list(range(11, 1000)); target=500')
13.306210007998743
>>> timeit('any(target == item for item in lst)', setup='lst = list(range(11, 1000)); target = 500')
24.3041033720001
As one could expect from short circuiting - huge differences in any() performance depending the location of the target.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 371 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  list in dicitonary element problem jacksfrustration 3 625 Oct-14-2023, 03:37 PM
Last Post: deanhystad
  Find (each) element from a list in a file tester_V 3 1,155 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,713 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,158 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,601 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  How to find the second lowest element in the list? Anonymous 3 1,901 May-31-2022, 01:58 PM
Last Post: Larz60+
  check if element is in a list in a dictionary value ambrozote 4 1,879 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,475 Apr-04-2022, 09:29 PM
Last Post: Larz60+
  Change a list to integer so I can use IF statement buckssg 3 2,194 Sep-21-2021, 02:58 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

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