#πŸ”’ how do i check if all elements of a list are there in another list

47 messages Β· Page 1 of 1 (latest)

misty cargo
#

tried this set(i) <= set(cords) but got error TypeError: unhashable type: 'list' I can loop through the list but i guess there is a better way

gusty adderBOT
#

@misty cargo

Python help channel opened

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.

shut shale
misty cargo
#

a list of 2 elemnts [x:int,y:int]

fast dock
#

If you have something like this:

a = [1,2,3]
b = [1,2,3,4,5]

you can check it with:

a in b

shut shale
#

!e

a = [1,2,3]
b = [1,2,3,4,5]

print(a in b)
gusty adderBOT
shut shale
#

you are trying to check if [1,2,3] as a whole is inside of the other list

fast dock
#

Oh sorry, my bad

shut shale
#

!e

a = [1,2,3]
b = [[1,2,3],4,5]

print(a in b)
gusty adderBOT
fast dock
#

Yeah right, then you can use:

set(a).issubset(b)

misty cargo
shut shale
gusty adderBOT
misty cargo
#

but for me

a=[[1,1],[0,0]]
b=[[1,1],[0,0],[1,5]]
shut shale
#

ahh , you have list inside of lists , that changes things

topaz wave
brisk robin
misty cargo
olive sierra
#

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))
gusty adderBOT
topaz wave
misty cargo
#

that can be done

olive sierra
misty cargo
#

would your code be more efficient than this

set(map(tuple, i)).issubset(map(tuple, cords))
olive sierra
#

!e
Oh then maybe the tuple approach isn't for you, py print(set((0, 1)) == set((1,0)))

gusty adderBOT
misty cargo
#

!e

print(set([(0, 1)]) == set([(1,0)]))
gusty adderBOT
misty cargo
olive sierra
#

Oh yeah mb

misty cargo
#

!e

print(set([(1,0)]) == set([(1,0)]))
gusty adderBOT
olive sierra
#

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?

misty cargo
misty cargo
#

thankyou all

gusty adderBOT
#
Python help channel closed

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