List to Tuple and Tuple to List?

D

Davy

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
 
P

Paul Hankin

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)
 
B

Boris Borcic

Davy said:
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
 
W

Wildemar Wildenburger

Davy said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top