Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Conversion of List of Tuples

Reply
Thread Tools

Conversion of List of Tuples

 
 
Alexander Blinne
Guest
Posts: n/a
 
      12-04-2012
Am 03.12.2012 20:58, schrieb :
> 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:

>>> from operator import add
>>> reduce(add, a)

(1, 2, 3, 4)

or

>>> reduce(operator.add, (list(t) for t in a))

[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
 
Reply With Quote
 
 
 
 
Hans Mulder
Guest
Posts: n/a
 
      12-04-2012
On 4/12/12 10:44:32, Alexander Blinne wrote:
> Am 03.12.2012 20:58, schrieb :
>> 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:
>
>>>> from operator import add
>>>> reduce(add, a)

> (1, 2, 3, 4)


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

>>> sum(a, ())

(1, 2, 3, 4)
>>>

> or
>
>>>> reduce(operator.add, (list(t) for t in a))

> [1, 2, 3, 4]


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



 
Reply With Quote
 
 
 
 
Neil Cerutti
Guest
Posts: n/a
 
      12-04-2012
On 2012-12-04, Hans Mulder <> wrote:
> It's considered bad style to use map it you don't want the list it
> produces.
>
>> There are more ways:
>>
>>>>> from operator import add
>>>>> reduce(add, a)

>> (1, 2, 3, 4)

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

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

--
Neil Cerutti
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dictionaries with tuples or tuples of tuples Jon Reyes Python 18 02-19-2013 03:56 AM
Passing tuples of tuples to sqlite xera121 Python 8 09-30-2009 05:45 AM
tuples within tuples korovev76@gmail.com Python 12 10-27-2007 08:16 PM
Different tuples to one container? (One type of a pointer to point to different kinds of tuples?) fff_afafaf@yahoo.com C++ 5 10-05-2006 11:17 PM
Converting a flat list to a list of tuples metiu uitem Python 31 11-24-2005 09:26 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57