Conversion of List of Tuples

Chris Angelico rosuav at gmail.com
Mon Dec 3 15:17:05 EST 2012


On Tue, Dec 4, 2012 at 7:04 AM, John Gordon <gordon at panix.com> wrote:
> In <6049bc85-6f8e-429b-a855-ecef47a9d28e at googlegroups.com> subhabangalore at gmail.com writes:
>
>> 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



More information about the Python-list mailing list