Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

How to compare two list elements???

How can one check if all the elements in one list are there is another??

I know that I can use a looping statement to check for each individual elements, but there has to be a method to do it??

example

list1 = [2,3,6] list2 = [2,3,4,5,6,7]

I tried using

if list1 in list2

but it always comes out as false

2 Answers

This is because Python is searching for that list as it is, 2, 3, 6 in sequence. It doesn't understand when 4 and 5 are in there too as the sequence 2,3,6 doesn't exist.

Ok but is there a way I can check if the elements in one list are in another?

Yes, you can use a loop to check:

list1 = [2, 3, 6]
list2 = [1, 2, 3, 4, 5, 6, 7]
for item in list1:
  if item in list2:
    #do something

Well heres in problem, I am trying to make a tic tac toe game,

I am storing all the possible winning sequences as a list of lists, and I am storing all the position of each player played in. So I need to check each list, using a for loop it becomes complicated. I did try to make one but not able to.

winSequence = [[1,2,3],[4,5,6],[7,8,9],[1,5,9],[3,5,7],[1,4,7],[2,5,8],[3,6,9]] player1 = [1,3,5,7] player2 = [2,4,8]