-1

I have a creative problem that I want to solve.

Let's say if I have two list as below. I want to compare if all elements in the req_param list are also in the full_list. I know it is easy to do the same using a for loop and getting the answer. But I am trying to figure out if there is a python3 in-built fxn to do the same..

req_param = ['ele1','ele2','ele3','ele4]
full_param = [['ele1','ele2','ele3','ele4','ele6'] 

During the comparison, I don't care if there are additional elements in full_param list. I just care that if full_param has all the elements of the req_param, then somehow I want to return it true else, I want to return it false.

Currently, this works with the for loop. But really think there should be an inbuilt fxn like compare. The most important part is that each element may not come in the same order, so I am ok to sort my list before passing it to a fxn...

Rupesh Desai
  • 171
  • 2
  • 5
  • The for loops looks something like this: \nfor keys in required_params: if keys not in formData: i += 1 return False if i > 0 else True – Rupesh Desai Apr 30 '20 at 14:50
  • 1
    Use `all()` and a list comprehension: `if all(item in full_list for item in req_param):` – John Gordon Apr 30 '20 at 14:51
  • 2
    Please format your code more carefully. It's obvious syntax error. – Maciek Apr 30 '20 at 14:52
  • 2
    Missing `'` and a `[` too many. – Patrick Artner Apr 30 '20 at 14:54
  • @RupeshDesai for loops are almost never the answer for something like list manipulation ... python has such great syntax, built-ins, and workarounds to eliminate regular for loops ... for example, JohnGordon 's reply – YulkyTulky Apr 30 '20 at 14:55
  • is it important that `[1,2,3]` is not the same as `[1,2,1,3,1]` ?c - if not set-operations are the way to go – Patrick Artner Apr 30 '20 at 14:55
  • Does this answer your question? [How can I verify if one list is a subset of another?](https://stackoverflow.com/questions/16579085/how-can-i-verify-if-one-list-is-a-subset-of-another) – wjandrea Apr 30 '20 at 15:11
  • BTW welcome to SO! Please read the [tour] and [ask]. Please do your own research before asking a question. If you google something like "python all elements of a list in another list" you'll find lots of results. – wjandrea Apr 30 '20 at 15:17
  • Yes I had assumed that this should be with some built-in fxn, thus the question. Although I agree, I should loop through such list manipulations... – Rupesh Desai May 01 '20 at 19:10

3 Answers3

1

As was mentioned there are several ways:

  1. Use all(): if all(item in full_list for item in req_param):
  2. Use set(): if set(req_param).issubset(set(full_param)):
wjandrea
  • 28,235
  • 9
  • 60
  • 81
0

I figured out a different way you can solve the problem.

You could just use set() and len() to solve the problem instead of for loop

Here's how:

r = ['ele1','ele2','ele3','ele4']
f = ['ele1','ele2','ele3','ele4','ele6']
print(len(set(r)-set(f))==0)
wjandrea
  • 28,235
  • 9
  • 60
  • 81
MrFamousKK
  • 34
  • 5
0

use all keyword , it returns True if all the conditions are satisfied else it returns False

Aashish
  • 5
  • 4