babikar
babikar

Reputation: 611

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,BC,CD],[a,b,c,d],[f,c,a,r],[i,s,r]]

Upvotes: 0

Views: 194

Answers (2)

jcao219
jcao219

Reputation: 2848

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("-"))

Upvotes: 1

thegrinner
thegrinner

Reputation: 12243

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

Upvotes: 0

Related Questions