-2

I have two lists:

my_list = [1,2,3,4,5]

my_new_list = [[1,3,7,5],[1,2,3,4,5]]

How can I check that a sublist equals my_list?

MSeifert
  • 145,886
  • 38
  • 333
  • 352
Computing Corn
  • 117
  • 5
  • 13

2 Answers2

2

If you want to check if my_list is in the my_new_list just use in:

>>> my_list in my_new_list
True

If you want to know the index of the matching list you can use index:

>>> my_new_list.index(my_list)
1 

If you think these are too efficient, too easy or too short you can do it manually as well:

>>> any(sublist == my_list for sublist in my_new_list) # equivalent to "in"
True

>>> next(idx for idx, sublist in enumerate(my_new_list) if sublist == my_list)  # like "index".
1
MSeifert
  • 145,886
  • 38
  • 333
  • 352
0

You can index built-in function

>>> my_new_list.index(my_list)
1

Or you can use in :

>>> my_list in my_new_list
True

You can also use magic function contains

>>> my_new_list.__contains__(my_list)
True
user3894045
  • 313
  • 3
  • 8