1

I have the given the following two lists:

list1 = ['', '', 'void KL15 ein', '{', '}', '', 'void Motor ein', '{', '}', '']
and
list2 = ['KL15 ein', 'Motor ein']

Now I want to find out if the a value from the second list 'KL15 ein' is also in the first list (as a void function). Do I need to extract the value 'KL15 ein' from both lists to compare it or is there another way to do it without extracting (via splitting and access the list) the values? Edit: I noticed that I need variables (can't use 'KL15 ein' as a string), e.g: if the value from list1 is also listed in list 2

Markus
  • 51
  • 6
  • Your question is not clear. What exactly do you mean by "*extract*"? – PM 77-1 Aug 31 '22 at 15:30
  • 2
    Does this answer your question? [How to check if a string is a substring of items in a list of strings](https://stackoverflow.com/questions/4843158/how-to-check-if-a-string-is-a-substring-of-items-in-a-list-of-strings) – Axe319 Aug 31 '22 at 15:36

2 Answers2

1

I think you can use the issubset() or all() function.

issubset()

if(set(subset_list).issubset(set(large_list))):
    flag = 1
 
# printing result
if (flag):
    print("Yes, list is subset of other.")
else:
    print("No, list is not subset of other.")

or all() function

if(all(x in test_list for x in sub_list)):
    flag = 1
 
# printing result
if (flag):
    print("Yes, list is subset of other.")
else:
    print("No, list is not subset of other.")
Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
1

First, get the set of void function names from the first list by checking which strings start with "void " and then stripping away that bit. Then, you can use a simple list comprehension, all expression or for loop to check which of the elements in the second list are in that set.

lst1 = ['', '', 'void KL15 ein', '{', '}', '', 'void Motor ein', '{', '}', '']
lst2 = ['KL15 ein', 'Motor ein']                                        
void_funs = {s[5:] for s in lst1 if s.startswith("void ")}              
res = [s in void_funs for s in lst2]                                          
# [True, True]

Or as a loop with a condition:

for s in lst2:
    if s in void_funs:
        # do something
tobias_k
  • 81,265
  • 12
  • 120
  • 179
  • How can I replace the statement `s[5:]` from the line `void_funs = {s[5:] for s in lst1 if s.startswith("void ")} `? As you can see, I edited the question and I can't use the static value (5th of the list). So I need a variable which contains the values of list2 and compare it with the values from list1 (the ones that startswith("void") should work. – Markus Sep 01 '22 at 07:16
  • 1
    @Markus `s[5:]` does not mean "the fifth value in the list" but "the substring starting at the fifth character", effectively stripping the `"void "` part off the functions leaving only the name. I don't see why that should not work. If e.g. the number of spaces after `void` can be variable, you might e.g. use `str.split` to split off the "void" part. – tobias_k Sep 01 '22 at 07:20
  • Ah ok I didn't know that, thanks. So how could I implement the last line as a if-loop? So if the the values are in the both list: do something? – Markus Sep 01 '22 at 08:37
  • Ok with `if [s in void_funs for s in list2` ist works. Thanks a lot! – Markus Sep 01 '22 at 09:55