Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a list of tuples into a simple flat list [duplicate]

Tags:

Possible Duplicate:
How do I convert a tuple of tuples to a one-dimensional list using list comprehension?

Let's say I have the following list of tuples:

[(1,2), (1,3), (1,4), (1,5), (1,6)] 

I am trying to convert it to a simple list like I have below:

[1,2,1,3,1,4,1,5,1,6] 

How do I convert it to a simple list like the one above without having to iterate through each element and add the items one by one to another list?

Are there any fast and efficient ways to do this without actually iterating through the original list of tuples, or is there some built-in function/method to do this?

like image 234
davidadamojr Avatar asked Sep 10 '12 16:09

davidadamojr


People also ask

Can list and tuple have duplicate values?

Tuples allow duplicate members and are indexed. Lists Lists hold a collection of objects that are ordered and mutable (changeable), they are indexed and allow duplicate members. Sets Sets are a collection that is unordered and unindexed. They are mutable (changeable) but do not allow duplicate values to be held.

Can you convert a tuple to a list?

Python list method list() takes sequence types and converts them to lists. This is used to convert a given tuple into list. Note − Tuple are very similar to lists with only difference that element values of a tuple can not be changed and tuple elements are put between parentheses instead of square bracket.

How do I flatten a list of lists?

Flattening a list of lists entails converting a 2D list into a 1D list by un-nesting each list item stored in the list of lists - i.e., converting [[1, 2, 3], [4, 5, 6], [7, 8, 9]] into [1, 2, 3, 4, 5, 6, 7, 8, 9] .


2 Answers

lst = [(1,2), (1,3), (1,4), (1,5), (1,6)]  import itertools list(itertools.chain(*lst)) # [1, 2, 1, 3, 1, 4, 1, 5, 1, 6] 

Alternatively:

[e for l in lst for e in l] # [1, 2, 1, 3, 1, 4, 1, 5, 1, 6] 
like image 197
David Robinson Avatar answered Sep 28 '22 04:09

David Robinson


Fundamentally, which one is faster? Using the "itertools" module, or using a list comprehension? I'm basically trying to improve my computation speed here. - @davidadamojr

I've been doing some tests and I find that the code below is actually faster.

list_ = [(1, 2), (1, 3), (1, 4), (1, 5), (1, 6)] list(sum(list_, ())) 

Someone correct me if I'm wrong.

Here are some tests below.

>>> list_ = [(1, 2), (1, 3), (1, 4), (1, 5), (1, 6)] >>>  >>> operation_1 = lambda: [tuple_item for tuple_ in list_ for tuple_item in tuple_] >>> def operation_2 ():         final_list = []         for tuple_ in list_:             for tuple_item in tuple_:                 final_list.append(tuple_item)         return final_list  >>> operation_3 = lambda: reduce(list.__add__, map(list, list_)) >>> def operation_4 ():         import itertools         return list(itertools.chain(*list_))  >>> operation_5 = lambda: list(sum(list_, ())) >>>  >>> operation_1() [1, 2, 1, 3, 1, 4, 1, 5, 1, 6] >>> operation_2() [1, 2, 1, 3, 1, 4, 1, 5, 1, 6] >>> operation_3() [1, 2, 1, 3, 1, 4, 1, 5, 1, 6] >>> operation_4() [1, 2, 1, 3, 1, 4, 1, 5, 1, 6] >>> operation_5() [1, 2, 1, 3, 1, 4, 1, 5, 1, 6] >>>  >>> import timeit >>>  >>> print('operation_1 completed in %s seconds.' % (timeit.timeit(operation_1))) operation_1 completed in 1.57890490223 seconds. >>> print('operation_2 completed in %s seconds.' % (timeit.timeit(operation_2))) operation_2 completed in 2.90350501659 seconds. >>> print('operation_3 completed in %s seconds.' % (timeit.timeit(operation_3))) operation_3 completed in 5.08437990236 seconds. >>> print('operation_4 completed in %s seconds.' % (timeit.timeit(operation_4))) operation_4 completed in 3.85125378138 seconds. >>> print('operation_5 completed in %s seconds.' % (timeit.timeit(operation_5))) operation_5 completed in 1.2623826489 seconds. 
like image 25
jonathanmarvens Avatar answered Sep 28 '22 04:09

jonathanmarvens