Beginner question: Converting Single-Element tuples to list

vdavidster at gmail.com vdavidster at gmail.com
Mon Jun 27 01:40:12 EDT 2005


Hi,

Thanks for your reply! A new thing learned....

Allow me to follow that up with another question:

Let's say I have a result from a module called pyparsing:

Results1 = ['abc', 'def']
Results2 = ['abc']

They are of the ParseResults type:

>>> type(Results1)
<class 'pyparsing.ParseResults'>
>>> type(Results2)
<class 'pyparsing.ParseResults'>

I want to convert them into Python lists. list() will work fine on
Results1, but on Results2, it will return:

['a', 'b', 'c']

Because 'abc' is a string. But I want it to return ['abc'].

In short, my question is: how do I typecast an arbitrary object,
whether a list-like object or a string, into a Python list without
having Python split my strings into characters?

Thanks very much for your help!

David

p.s. I know pyparsing has an asList() method, but let's just say I am
unable to use it in my circumstances.




More information about the Python-list mailing list