Beginner question: Converting Single-Element tuples to list

Reinhold Birkenfeld reinhold-birkenfeld-nospam at wolke7.net
Mon Jun 27 02:07:58 EDT 2005


vdavidster at gmail.com wrote:
> 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?

This seems like a glitch in pyparsing. If a ParseResults class emulates
a list, it should do so every time, not only if there is more than one
value in it.

Reinhold



More information about the Python-list mailing list