Function/method returning list of chars in string?

  • Thread starter Hendrik van Rooyen
  • Start date
H

Hendrik van Rooyen

One can go from lb = ['b','a','n','a','n','a']
to s = "banana" by using s = "".join(lb)

Is there a way to go the reverse route?

I have not been able to find one.

It is obviously easy to write a for char in s loop
or list comprehension, but there seems to be
no function or string method to return a list
of characters.

using lb = s.split("") would have been nice
as a complement to s = "".join(lb).

Split is already full of magic, it could do with more.

- Hendrik
 
D

Diez B. Roggisch

Hendrik said:
One can go from lb = ['b','a','n','a','n','a']
to s = "banana" by using s = "".join(lb)

Is there a way to go the reverse route?

I have not been able to find one.

It is obviously easy to write a for char in s loop
or list comprehension, but there seems to be
no function or string method to return a list
of characters.

using lb = s.split("") would have been nice
as a complement to s = "".join(lb).

Split is already full of magic, it could do with more.


I think

lb = list(s)

is good enough.

Diez
 
D

Diez B. Roggisch

Hendrik said:
It does the job, of course, but it is not a string method.

And that is a bad thing because of what?

Also as list-comps are going away and are replaced by

list(<generator-expression>)

I'd say it's much more consistent when faced with the task of creating a
list of anything that's iterable one uses the

list(<iterable>)

idiom.

Diez
 
T

Terry Reedy

Miles said:
Where did you hear that?

Perhaps in the discussion of possible changes for 3.0 about 18 months
ago. The idea was rejected because set/list/dict(genexp) is too much
slower (say 2x) than the direct implementation of set/list/dict
comprehensions.

tjr
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top