##### using element on a list as condition statement
 using element on a list as condition statement ndc85430 Da Bishop Posts: 1,152 Threads: 0 Joined: Apr 2017 Reputation: 76 May-21-2020, 07:47 AM (This post was last modified: May-21-2020, 07:47 AM by ndc85430.) (May-20-2020, 12:12 PM)hussainmujtaba Wrote: 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. This really isn't correct reasoning. Consider a list of length n containing unique items. The performance of the search isn't going to be the same as for a set. With a list, you either have to do a search through the entire thing to find the item you want (which at worst takes time proportional to n, hence why this is called a linear search) or if the list is sorted, perform a binary search (which at worst takes time proportional to log_2 n). Sets are based on hash tables, meaning that items are stored in buckets based on their hash value (a number representing them). Searching for an item means computing its hash (which should be fast!) and then using that as an index into this table, which is an array-like thing, meaning that looking up something by index (and consequently search) is done in constant time (i.e. independent of n). I suspect the constants of proportionality here mean that the difference is going to be noticeable only for sufficiently large n. Reply hussainmujtaba Silly Frenchman Posts: 43 Threads: 0 Joined: Apr 2020 Reputation: 3 May-21-2020, 12:53 PM (May-20-2020, 02:40 PM)perfringo Wrote: (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 am pretty much sure that the both methods will take the exactly the same time to execute.You can check practically if you want Reply

 Possibly Related Threads… Thread Author Replies Views Last Post Sume Every 10 element in the list and Divided of Sum quest_ 7 139 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 124 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 97 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 436 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