-2

Is there a function that returns the arrays that are in a list according to the search?

For example I want to get the list of tables containing the letter A

myLst = [[ABC], [BCV], [BAD]]
return [[ABC], [BAD]]

Do I have to make my own function ?

Dimitris Fasarakis Hilliard
  • 150,925
  • 31
  • 268
  • 253
Jimmy Gonçalves
  • 79
  • 1
  • 2
  • 9

2 Answers2

0

Very possible, and simple, just do as follows

if x for x in list if 'a' in x:
     #Do something

It's simple list comprehension, I recommend reading Python MDN before starting to code

Dwad
  • 165
  • 1
  • 7
0

You can do in one line :

print([item for item in myLst for sub_item in item if 'A' in sub_item])

output:

[['ABC'], ['BAD']]

or as you said you want a function so here is the detailed solution :

def return_list(list_1):
    result=[]
    for item in list_1:
        if isinstance(item,list):
            for sub_item in item:
                if 'A' in sub_item:
                    result.append(item)
    return result

print(return_list(myLst))
Aaditya Ura
  • 12,007
  • 7
  • 50
  • 88