1

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.

  • It's not clear (to me) how your input looks like. Maybe adding an MRE would be a good idea (see [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example))? – Timus Apr 30 '22 at 15:54
  • Try running through this with a [debugger](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to see what's going on. – Alias Cartellano Apr 30 '22 at 21:49
  • @I'm going to pass this one an send them a ticket. Because the programm if the Items are not in the same order they dont score it – GonlinNocturno May 01 '22 at 09:38

0 Answers0