4

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")
3
  • 2
    Your code will output absent twice and present once. Is that intentional? Commented Oct 3, 2021 at 7:37
  • 1
    That is what am trying to avoid, I just want to know if present and if not present once, let me check out the answers. Commented Oct 3, 2021 at 7:40
  • 2
    If that's only what you're trying to solve, then you should check the for/else construct. You basically need to add a break in the if and unindent the else. Then, the answers below still simplify the loop in a nice way. Commented Oct 3, 2021 at 7:44

6 Answers 6

7

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")
Sign up to request clarification or add additional context in comments.

Comments

3

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.

2 Comments

If anything, use a generator for the second option. That way you will have the short-circuiting and not waste memory for a spurious list.
@schwobaseggl Indeed that would be better. I will leave it like that as your answer is cleaner in that aspect using the map (got my upvote)
3

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

Comments

2

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

Comments

2

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

1 Comment

What is value here? Is it the loop variable? And this code should be inside the loop? In that case this is exactly the same as the original code...
2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.