Reputation: 1109
I have this running well, however am unable to avoid the loop, how can I get an only True
if present or False
if not present without having to loop through the list using a for loop?
my_list = [[1, 2], [4, 6], [8, 3]]
combined = [3, 8]
for value in my_list:
print(value)
if set(combined) == set(value):
print("present")
else:
print("absent")
Upvotes: 4
Views: 410
Reputation: 4874
You could use recursion:
def is_present(seq, elem, n=None):
if n is None:
n = len(seq) - 1
if n == -1:
return False
if set(seq[n]) == set(elem):
return True
return is_present(seq, elem, n-1)
if is_present(my_list, combined):
print("Present")
else:
print("Not present")
There is also a problem with your existing code. You shouldn't have an else
inside the loop body because if the very first element in my_list
is not equal to combined
, it will print Not present
even though it may be present in the list later. And it will do this for every element that is not equal to it. This can be fixed with a for
-else
statement:
my_list = [[1, 2], [4, 6], [8, 3]]
combined = [3, 8]
for value in my_list:
print(value)
if set(combined) == set(value):
print("present")
break
else:
print("absent")
Or you can just wrap it in a function:
def is_present():
my_list = [[1, 2], [4, 6], [8, 3]]
combined = [3, 8]
for value in my_list:
print(value)
if set(combined) == set(value):
return True
return False
Upvotes: 2
Reputation: 19414
To avoid the multiple absent
printing, you can use a for/else
construct:
for value in my_list:
if set(combined) == set(value):
print("present")
break
else:
print("absent")
But if you're looking to optimize/reduce the loop, you can "hide" the loop inside an any
call:
any(set(combined) == set(value) for value in my_list)
Or alternatively use a list comprehension to convert all sub-lists to sets:
set(combined) in [set(sublist) for sublist in my_list]
But as you can see, some sort of looping is necessary. I believe that by removing the loop you meant to reduce its code size.
Upvotes: 3
Reputation: 21
From your problem statement I am able to understand is that you just want to find out that the list in combined inside the 2D list i.e my_list
Solution- you could directly use the the following block of code to get your result;
print("present") if set(combined) == set(value) else print("absent")
Upvotes: 2
Reputation: 71570
I would prefer a generator with next
:
next(("present" for value in my_list if set(value) == set(combined)), 'absent')
Or with any
:
['absent', 'present'][any(set(combined) == set(value) for value in my_list)]
Or even better with map
and in
:
['absent', 'present'][set(combined) in map(set, my_list)]
All output:
present
Upvotes: 3
Reputation: 73450
You can do this rather concisely, getting the short-circuiting of any
and the brevity of map
all while avoiding ugly lambda
or barely readable conditional expressions:
if set(combined) in map(set, my_list):
print("present")
else:
print("absent")
Upvotes: 7
Reputation: 13061
Using built-in function any
and class map
,
def is_inside(item, items):
return any(map(lambda x:set(x)==set(combined), my_list))
my_list = [[1, 2], [4, 6], [8, 3]]
combined = [3, 8]
if is_inside(combined, my_list):
print("present")
else:
print("absent")
Upvotes: 2