Python Forum
using element on a list as condition statement - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: using element on a list as condition statement (/thread-26931.html)

Pages: 1 2


using element on a list as condition statement - glennford49 - May-19-2020

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")
    



RE: using element on a list as condition statement - menator01 - May-19-2020

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



RE: using element on a list as condition statement - pyzyx3qwerty - May-19-2020

(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 ==


RE: using element on a list as condition statement - glennford49 - May-19-2020

(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


RE: using element on a list as condition statement - buran - May-19-2020

Don't use sum as variable name, it's a built-in function


RE: using element on a list as condition statement - hussainmujtaba - May-20-2020

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')



RE: using element on a list as condition statement - perfringo - May-20-2020

(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.


RE: using element on a list as condition statement - buran - May-20-2020

(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



RE: using element on a list as condition statement - perfringo - May-21-2020

(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.


RE: using element on a list as condition statement - buran - May-21-2020

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