Skip to content Skip to sidebar Skip to footer

Lists And Sublists

say i have an output of this, i think its a list ['', 'AB-a-b-c-d', 'BC-f-c-a-r', 'CD-i-s-r'] i want to make the following: ['',[AB,a,b,c,d],[BC,f,c,a,r],[CD,i,s,r]] or ['',[AB,

Solution 1:

newlist = [item.split("-") for item in oldlist]

or (this works better because the empty string is kept as is)

newlist = []
for item in oldlist:
    if not item:
        newlist.append(item)
    else:
        newlist.append(item.split("-"))

Solution 2:

I'll try to point you in the right direction rather than solve your homework for you: Try split and some for loops.

Post a Comment for "Lists And Sublists"