-1

I need a code to find if "numbers" list in "allnumbers" list. if yes I want python to print out "yes", if no "no".

"numbers" list is in "allnumbers" list.

Can anyone help me?

numbers=[1,25,31,42,45,52,59,63,66,70]
allnumbers=[1,2,9,25,26,30,31,35,42,49,45,51,52,55,59,60,63,65,66,70]
omer
  • 27
  • 5

2 Answers2

4

As your variable names already suggested, you may use all(...):

numbers = [1, 25, 31, 42, 45, 52, 59, 63, 66, 70]
allnumbers = [1, 2, 9, 25, 26, 30, 31, 35, 42, 49, 45, 51, 52, 55, 59, 60, 63, 65, 66, 70]

if (all(number in allnumbers for number in numbers)):
    print("Yes")

Another variant would include using sets:

smaller_list = set(numbers)
bigger_list  = set(allnumbers)

if biggerlist.intersection(smaller_list) == smaller_list:
    print("yes")

To see, how many numbers have matched, use:

numbers_matched = len(bigger_list.intersection(smaller_list))

See a demo on ideone.com.

Jan
  • 42,290
  • 8
  • 54
  • 79
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/218039/discussion-between-omer-and-jan). – omer Jul 17 '20 at 14:26
1

Your Answer- A More Simple And Readable One

numbers, allnumbers=[1,25,31,42,45,52,59,63,66,70], [1,2,9,25,26,30,31,35,
                                                 42,49,45,51,52,55,59,
                                                 60,63,65,66,70]

# To Get The 'set' version of the list to later compare them
set_A, set_B  = set(numbers), set(allnumbers)

# Checking The Difference with '.difference'
result = set_B.difference(set_A)


# If There Is Some Result Of difference than the lists are not matching
if result:
    print("No Matching Elements Found!")
# Else All The Numbers Are In That List
else:
    print("Matching Elements Found")

Hope This Helps


Happy Coding!

Aryan
  • 1,093
  • 9
  • 22
  • even if the numbers are matching the output is "No Matching Elements Found!" but when i changed the code like `if not result:` it worked – omer Jul 17 '20 at 14:25