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
#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


Messages In This Thread
RE: using element on a list as condition statement - by perfringo - May-20-2020, 02:40 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  element in list detection problem jacksfrustration 5 358 Apr-11-2024, 05:44 PM
Last Post: deanhystad
  unable to remove all elements from list based on a condition sg_python 3 442 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  list in dicitonary element problem jacksfrustration 3 699 Oct-14-2023, 03:37 PM
Last Post: deanhystad
  Find (each) element from a list in a file tester_V 3 1,214 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,837 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,211 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,666 Jun-15-2022, 12:00 PM
Last Post: new_coder_231013
  How to find the second lowest element in the list? Anonymous 3 2,010 May-31-2022, 01:58 PM
Last Post: Larz60+
  check if element is in a list in a dictionary value ambrozote 4 1,967 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,515 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