By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,772 Members | 919 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,772 IT Pros & Developers. It's quick & easy.

List to Tuple and Tuple to List?

P: n/a
Hi all,

I am curious about whether there is function to fransform pure List to
pure Tuple and pure Tuple to pure List?

For example,

I have list L = [[1,2,3],[4,5,6]]
something list2tuple() will have T=list2tuple(L)=((1,2,3),(4,5,6))

And the tuple2list()

Any suggestions are welcome!

Best regards,
Davy

Nov 6 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Nov 6, 11:18 am, Davy <zhushe...@gmail.comwrote:
Hi all,

I am curious about whether there is function to fransform pure List to
pure Tuple and pure Tuple to pure List?

For example,

I have list L = [[1,2,3],[4,5,6]]
something list2tuple() will have T=list2tuple(L)=((1,2,3),(4,5,6))

And the tuple2list()
Assuming you only want to look inside the types that you're
replacing...

def transform(source, from_type, to_type):
if not isinstance(source, from_type):
return source
else:
return to_type(transform(x, from_type, to_type)
for x in source)

def list2tuple(source):
return transform(source, list, tuple)

def tuple2list(source):
return transform(source, tuple, list)

--
Paul Hankin
Nov 6 '07 #2

P: n/a
Davy wrote:
Hi all,

I am curious about whether there is function to fransform pure List to
pure Tuple and pure Tuple to pure List?

For example,

I have list L = [[1,2,3],[4,5,6]]
something list2tuple() will have T=list2tuple(L)=((1,2,3),(4,5,6))

And the tuple2list()

Any suggestions are welcome!
D = { list : tuple, tuple : list }

F = lambda x : D[type(x)](map(F,x)) if type(x) in D else x

list2tuple = tuple2list = F
Nov 6 '07 #3

P: n/a
Davy wrote:
Hi all,

I am curious about whether there is function to fransform pure List to
pure Tuple and pure Tuple to pure List?
Isn't that just the same topic as in your other thread? I think it is
somewhat unfriendly that you ignore that one. It makes me feel that you
see this group as some sort of helpdesk.

Which it isn't (strictly).

/W
Nov 7 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.