##### using element on a list as condition statement
 using element on a list as condition statement glennford49 Silly Frenchman Posts: 27 Threads: 11 Joined: Mar 2020 Reputation: 0 May-19-2020, 09:07 AM 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 menator01 Minister of Silly Walks Posts: 359 Threads: 53 Joined: Sep 2019 Reputation: 21 May-19-2020, 09:15 AM (This post was last modified: May-19-2020, 09:15 AM by menator01.) ```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`` Reply pyzyx3qwerty Minister of Silly Walks Posts: 356 Threads: 13 Joined: Mar 2020 Reputation: 9 May-19-2020, 09:17 AM (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 == Reply glennford49 Silly Frenchman Posts: 27 Threads: 11 Joined: Mar 2020 Reputation: 0 May-19-2020, 09:31 AM (This post was last modified: May-19-2020, 09:31 AM by glennford49.) (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 Posts: 6,938 Threads: 116 Joined: Sep 2016 Reputation: 538 May-19-2020, 10:17 AM 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 hussainmujtaba Silly Frenchman Posts: 43 Threads: 0 Joined: Apr 2020 Reputation: 3 May-20-2020, 12:12 PM (This post was last modified: May-20-2020, 12:13 PM by hussainmujtaba.) 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 perfringo Da Bishop Posts: 1,592 Threads: 7 Joined: Jun 2018 Reputation: 154 May-20-2020, 02:40 PM (This post was last modified: May-20-2020, 02:40 PM by perfringo.) (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 Posts: 6,938 Threads: 116 Joined: Sep 2016 Reputation: 538 May-20-2020, 03:20 PM (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 perfringo Da Bishop Posts: 1,592 Threads: 7 Joined: Jun 2018 Reputation: 154 May-21-2020, 12:54 AM (This post was last modified: May-21-2020, 12:54 AM by perfringo.) (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 Posts: 6,938 Threads: 116 Joined: Sep 2016 Reputation: 538 May-21-2020, 04:00 AM (This post was last modified: May-21-2020, 04:00 AM by buran.) 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 Sume Every 10 element in the list and Divided of Sum quest_ 7 135 Yesterday, 10:58 AM Last Post: perfringo Counting Element in Multidimensional List quest_ 1 91 Nov-25-2020, 10:00 PM Last Post: quest_ Adding List Element if Second part of the List Elements are the Same quest_ 3 121 Nov-25-2020, 04:33 PM Last Post: bowlofred Removing element from list squall 6 171 Nov-22-2020, 09:34 PM Last Post: jefsummers 2d Array adds last element to entire list waiteup 2 95 Nov-19-2020, 08:25 PM Last Post: bowlofred How to get index of minimum element between 3 & 8 in list Mekala 2 162 Nov-10-2020, 12:56 PM Last Post: DeaD_EyE Problem printing last element from a list tester_V 3 196 Oct-30-2020, 04:54 AM Last Post: tester_V Finding an element in a 1d list in a 2d array lionrocker221 0 372 Jun-27-2020, 04:50 PM Last Post: lionrocker221 removing dictionary element in list using (key, value) MelonMusk 3 435 Jun-13-2020, 02:37 PM Last Post: buran else condition not called when if condition is false Sandz1286 10 1,294 Jun-05-2020, 05:01 PM Last Post: ebolisa

Forum Jump:

### User Panel Messages

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