#π how do i check if all elements of a list are there in another list
47 messages Β· Page 1 of 1 (latest)
@misty cargo
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
what are the contents of your list (which you try to convert to set ) ?
a list of 2 elemnts [x:int,y:int]
If you have something like this:
a = [1,2,3]
b = [1,2,3,4,5]
you can check it with:
a in b
really thanks
!e
a = [1,2,3]
b = [1,2,3,4,5]
print(a in b)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
False
you are trying to check if [1,2,3] as a whole is inside of the other list
Oh sorry, my bad
!e
a = [1,2,3]
b = [[1,2,3],4,5]
print(a in b)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
True
Yeah right, then you can use:
set(a).issubset(b)
doesnt work
!e
a = [1,2]
b = [1,2,3]
print(set(a).issubset(b))
:white_check_mark: Your 3.12 eval job has completed with return code 0.
True
but for me
a=[[1,1],[0,0]]
b=[[1,1],[0,0],[1,5]]
ahh , you have list inside of lists , that changes things
That's the same as <=.
integers are hashable, which means they can be in sets. Lists are not hashable, so they can't be in sets.
thats fine but how do i check if a is in b ? looping though a and check if each element is in b is the only option
You can use the all built-in too, to make it clearer
!e
a=[[1,1],[0,0]]
b=[[1,1],[0,0],[1,5]]
print(all(x in b for x in a))
:white_check_mark: Your 3.12 eval job has completed with return code 0.
True
thanks
if you were to turn those sublists into tuples, sets would work.
that can be done
Do note for this approach though, that [0, 1] != [1,0]. I'm not sure that's what you want?
ya that is what i want
would your code be more efficient than this
set(map(tuple, i)).issubset(map(tuple, cords))
!e
Oh then maybe the tuple approach isn't for you, py print(set((0, 1)) == set((1,0)))
:white_check_mark: Your 3.12 eval job has completed with return code 0.
True
!e
print(set([(0, 1)]) == set([(1,0)]))
:white_check_mark: Your 3.12 eval job has completed with return code 0.
False
this is what i want
Oh yeah mb
!e
print(set([(1,0)]) == set([(1,0)]))
:white_check_mark: Your 3.12 eval job has completed with return code 0.
True
Then yours is better (I think?) because tuples are immutable so there isn't as much overhead? Maybe? Idk though
Oh but you're converting to tuples on the fly, so that might be a bit more work if the list is huge
Can't you have them always be tuples, since the length is always 2?
hmm idk I usally never use tuples as its not mutable but might use in this context as i wont change the cords
ya they are cordinates of my point
thankyou all
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.
π how do i check if all elements of a list are there in another list