471,310 Members | 1,375 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,310 software developers and data experts.

List to Tuple and Tuple to List?

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
3 1747
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
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
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.

Similar topics

23 posts views Thread by Fuzzyman | last post: by
9 posts views Thread by Yomanium Yoth Taripoät II | last post: by
4 posts views Thread by GrelEns | last post: by
2 posts views Thread by Ishwar Rattan | last post: by
16 posts views Thread by flyaflya | last post: by
5 posts views Thread by Xah Lee | last post: by
reply views Thread by Hatem Nassrat | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.