Question: Iterate through the list so that if the character ‘m’ is in the string, then it should be added to a new list called m_list. Hint: Because this isn’t just a list of lists, think about what type of object you want your data to be stored in. Conditionals may help you.
I'm not sure what I'm doing wrong, I've tried storing each sublst in a variable, and looping with a For
, but is not pythonic.
I've come across wit this solution:
def find_char(char, words):
m_list = []
for word in words:
if isinstance(word,list):
m_list += find_char(char,word)
else:
if char in word:
m_list.append(word)
return m_list
find_char('m', d)
Which the programm doesn`t accept as is a f. So, I cannot score the result.
I've come across with this idea:
m_list = []
for item in d:
if isinstance(item, str):
if 'm' in item:
m_list.append(item)
continue
for inner in item:
for sub_inner in inner:
if 'm' in sub_inner:
m_list.append(sub_inner)
Why, the second for loop for inner in item
doesn`t loop through both sublist's ?
I've tried everything, using map
, filter
, storing each sublist in a var so I can use a for loop directly ( bear in mind, it works but is not pythonic and I want to learn proper code. I can't think of anything else.