The Opposite of list(a,b,c)?
Daniel Berlin
dan at cgsoftware.com
Fri Apr 14 20:04:47 EDT 2000
On Fri, 14 Apr 2000, Andrew P. Jewell wrote:
<snip>
> def func(SearchTerm):
> <...>
> retval = []
> for keyval in (dtnA.keys()):
> mtch = re.search(patSrchTrm, keyval)
> if mtch:
> retval.append(UNLIST(dtnA[keyval]))
Change this to:
if mtch:
for value in dtnA[keyval]:
retval.append(value)
>
> I want to end up with just a list of matching values from the dictionary,
> ala:
> ['val1, val2', 'val3, val4']
>
> But I can only get a list of lists: [[val1,val2],[val3,val4]] - which
> makes perfect sense based on how I'm doing it - but I don't WANT a list
> of lists! Is there anyway to do this? I don't really want to tuple-ize
> my dictionary since it's attached to its list values in other ways. It
> seems ugly to have to iterate my dictionary value list to UN-list them
> manually - and the repr deal is REALLY ugly. What am I missing?? Thanks
> for any help!
Okay, you don't want to iterate?
Let something else iterate for you:
>>> def delist(x, retval):
... import types
... for a in x:
... if type(a)==types.ListType:
... delist(a,retval)
... else:
... retval.append(a)
... return retval
...
>>> delist([5,6],[])
[5, 6]
>>> delist([5,6,[5,6]],[])
[5, 6, 5, 6]
>>> delist([5,6,[5,6],[5,[5,5]]],[])
[5, 6, 5, 6, 5, 5, 5]
>>>
etc
--Dan
>
> Andy
>
More information about the Python-list
mailing list