It's python for loop is not working pls tell me what's wrong in it

It's python for loop is not working pls tell me what's wrong in it
0

#1
list_d = ["Mic", "Phone", 323.12, 3123.123, "Justin", "Bag", "Cliff Bars", 134]



def prase_list_it(xyz):
    a=[]
    b=[]
    for i in xyz:
        if isinstance(i,float) or isinstance(i,int):
            a.append(i)
        elif isinstance(i,str):
            b.append(i)
        else:
            pass
        return b,a

print( prase_list_it(list_d))

OUTPUT
([‘Mic’], [])

------OUTPUT Wanted
([‘Mic’, ‘Phone’, ‘Justin’, ‘Bag’, ‘Cliff Bars’], [323.12, 3123.123, 134])


#2

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard.

The way your code is currently written, you return b,a inside the for loop. You need to move it to outside the for loop.

If you know in advance that the list will ONLY consist of strings and numbers, then the following would work:

def prase_list_it(xyz):
  a = []; b = []
  for i in xyz:
    a.append(i) if not isinstance(i,str) else b.append(i)
  return b,a

#3

Thanks :grinning:

It’s great help