Conversion of List of Tuples

S

subhabangalore

Dear Group,

I have a tuple of list as,

tup_list=[(1,2), (3,4)]
Now if I want to covert as a simple list,

list=[1,2,3,4]

how may I do that?

If any one can kindly suggest? Googling didn't help much.

Regards,
Subhabrata.
 
C

Chris Kaynor

Dear Group,

I have a tuple of list as,

tup_list=[(1,2), (3,4)]
Now if I want to covert as a simple list,

list=[1,2,3,4]

how may I do that?

If any one can kindly suggest? Googling didn't help much.

If you know they are always exactly two levels deep, you can use
nested loops (in comprehension form):
[item for tuple_ in list_ for item in tuple_]

That could also be written how John recommended, in three lines.
 
C

Chris Angelico

In said:
Dear Group,
I have a tuple of list as,
tup_list=[(1,2), (3,4)]
Now if I want to covert as a simple list,
list=[1,2,3,4]

how may I do that?

new_list = []

for t in tup_list:
for item in t:
new_list.append(item)

Which can be written more succintly as:

new_list = []
for t in tup_list:
new_list.extend(t)

In more general terms, what you're looking to do here is *flatten*
your structure. Not sure if that would have helped in the web search
that you doubtless did before asking this question. :)

ChrisA
 
S

subhabangalore

Dear Group,



I have a tuple of list as,



tup_list=[(1,2), (3,4)]

Now if I want to covert as a simple list,



list=[1,2,3,4]



how may I do that?



If any one can kindly suggest? Googling didn't help much.



Regards,

Subhabrata.

Thanks. But I am not getting the counter "5posts 0 views"...if moderator can please check the issue.
 
J

John Gordon

In said:
Thanks. But I am not getting the counter "5posts 0 views"...if
moderator can please check the issue.

I logged in via Google Groups and all the replies were present. What
is your question?

(This group is not moderated.)
 
S

Steven D'Aprano

Thanks. But I am not getting the counter "5posts 0 views"...if moderator
can please check the issue.

What counter are you talking about?

This is an email mailing list, also copied to the Usenet newsgroup
comp.lang.python, and mirrored on other places including gmane and
various web sites. Neither email nor Usenet include "counters", so you
will have to explain what you are talking about.
 
W

Walter Hurry

What counter are you talking about?

This is an email mailing list, also copied to the Usenet newsgroup
comp.lang.python, and mirrored on other places including gmane and
various web sites. Neither email nor Usenet include "counters", so you
will have to explain what you are talking about.

Doubtless he is talking about G**gle Groups, since I don't see his posts
anyway.
 
A

Alexander Blinne

Am 03.12.2012 20:58, schrieb (e-mail address removed):
Dear Group,

I have a tuple of list as,

tup_list=[(1,2), (3,4)]
Now if I want to covert as a simple list,

list=[1,2,3,4]

how may I do that?

Another approach that has not yet been mentioned here:
a=[(1,2), (3,4)]
b=[]
map(b.extend, a) [None, None]
b
[1, 2, 3, 4]

map returns [None, None] because extend returns nothing, but now
b==[1,2,3,4].

There are more ways:
(1, 2, 3, 4)

or
[1, 2, 3, 4]

I didn't do any performance testing, i guess the first one should be
about as fast es the for-loop approach with .extend() and the other two
might be quite slow. Although this only really matters if you have large
lists.

Greetings
 
H

Hans Mulder

Am 03.12.2012 20:58, schrieb (e-mail address removed):
Dear Group,

I have a tuple of list as,

tup_list=[(1,2), (3,4)]
Now if I want to covert as a simple list,

list=[1,2,3,4]

how may I do that?

Another approach that has not yet been mentioned here:
a=[(1,2), (3,4)]
b=[]
map(b.extend, a) [None, None]
b
[1, 2, 3, 4]

map returns [None, None] because extend returns nothing, but now
b==[1,2,3,4].

It's considered bad style to use map it you don't want the list it
produces.
There are more ways:

(1, 2, 3, 4)

There's a built-in that does "reduce(operator.add"; it's called "sum":

This is a valid use case for the map operator:
sum(map(list, a), []) [1, 2, 3, 4]
I didn't do any performance testing, i guess the first one should be
about as fast as the for-loop approach with .extend() and the other two
might be quite slow. Although this only really matters if you have large
lists.

Hope this helps,

-- HansM
 
N

Neil Cerutti

It's considered bad style to use map it you don't want the list it
produces.


There's a built-in that does "reduce(operator.add"; it's called "sum":

(1, 2, 3, 4)

I thought that sort of thing would cause a warning. Maybe it's
only for lists.

Here's the recipe from the itertools documentation:

def flatten(listOfLists):
"Flatten one level of nesting"
return chain.from_iterable(listOfLists)
 

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

Forum statistics

Threads
473,961
Messages
2,570,131
Members
46,689
Latest member
liammiller

Latest Threads

Top