Python Forum
Thread Rating:
  • 2 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Recursivity - Problem
#1
Hi, I got a problem this code :

def deepconcat(l) :
    if l == [] :
        return None
    elif len(l) == 1 :
        return l[0]
    else :
        for i in l :
            if type(i) == str :
                return l[0] + deepconcat(l[1:])
            if type(i) == list :
                return deepconcat(i+l[1:]) 
I have to turn ["a",["b","6"],"e",["5",["g","h"]],"i"] into "ab6e5ghi".
It works.
If I add a string at the end of the list, for example : ["a",["b","6"],"e",["5",["g","h"]],"i","o"], it works.
But when I add a sub list, for example : ["a",["b","6"],"e",["5",["g","h"]],"i",["k"]], I have a TypeError.
I don't understand why, could you please help me ?
Reply


Messages In This Thread
Recursivity - Problem - by SupaFlamme - Jan-14-2019, 09:48 PM
RE: Recursivity - Problem - by nilamo - Jan-14-2019, 10:10 PM
RE: Recursivity - Problem - by micseydel - Jan-14-2019, 11:35 PM
RE: Recursivity - Problem - by scidam - Jan-15-2019, 12:33 AM
RE: Recursivity - Problem - by micseydel - Jan-15-2019, 12:57 AM
RE: Recursivity - Problem - by scidam - Jan-15-2019, 01:15 AM
RE: Recursivity - Problem - by perfringo - Jan-15-2019, 09:49 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020