Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Conversion of List of Tuples (http://www.velocityreviews.com/forums/t955077-conversion-of-list-of-tuples.html)

 subhabangalore@gmail.com 12-03-2012 07:58 PM

Conversion of List of Tuples

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.

 John Gordon 12-03-2012 08:04 PM

Re: Conversion of List of Tuples

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

--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 Gary Herron 12-03-2012 08:08 PM

Re: Conversion of List of Tuples

On 12/03/2012 11:58 AM, subhabangalore@gmail.com wrote:
> [(1,2), (3,4)]
>>> L=[(1,2), (3,4)]
>>>
>>> [b for a in L for b in a]

[1, 2, 3, 4]

--
Dr. Gary Herron
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418

 Chris Kaynor 12-03-2012 08:10 PM

Re: Conversion of List of Tuples

On Mon, Dec 3, 2012 at 11:58 AM, <subhabangalore@gmail.com> wrote:
> 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.

>
> Regards,
> Subhabrata.
> --
> http://mail.python.org/mailman/listinfo/python-list

 MRAB 12-03-2012 08:13 PM

Re: Conversion of List of Tuples

On 2012-12-03 20:04, John Gordon wrote:
>
>> 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)
>

Or you could use .extend:

new_list = []

for t in tup_list:
new_list.extend(t)

 Chris Angelico 12-03-2012 08:17 PM

Re: Conversion of List of Tuples

On Tue, Dec 4, 2012 at 7:04 AM, John Gordon <gordon@panix.com> wrote:
>
>> 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

 subhabangalore@gmail.com 12-03-2012 09:14 PM

Re: Conversion of List of Tuples

On Tuesday, December 4, 2012 1:28:17 AM UTC+5:30, subhaba...@gmail.com wrote:
> 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.

 John Gordon 12-03-2012 10:02 PM

Re: Conversion of List of Tuples

> 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

(This group is not moderated.)

--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 Steven D'Aprano 12-03-2012 10:11 PM

Re: Conversion of List of Tuples

On Mon, 03 Dec 2012 13:14:19 -0800, subhabangalore wrote:

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

--
Steven

 Walter Hurry 12-03-2012 10:43 PM

Re: Conversion of List of Tuples

On Mon, 03 Dec 2012 22:11:40 +0000, Steven D'Aprano wrote:

> On Mon, 03 Dec 2012 13:14:19 -0800, subhabangalore wrote:
>
>> 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.

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

All times are GMT. The time now is 12:08 AM.