## python:[[1,2],[3,4],[5,6],[7,8]] transform into [[1],[2,3],[4,5],[6,7],[8]] and vice versa

Question!

my current solution-pointers would be

• ether via a iterator class which yields the new assembled inner lists
• or via a iter function which yields the new assembled inner lists

is there another, better way to solve this challenge?

Edit

@Glenn: good objection. I wasn't thinking of that because I experienced lists not ordered in the manner I thought.

@THC4k: thank you for your solution. I learned chain.from_iterable

@Mike DeSimone: Hmm tested your solution but something went wrong maybe I missed something yet,...

@Jamie and Odomontois: Thank you for pointing out to be more detailed

my goal

I am forging a small algorithm which transforms a list of tasks – pairs/tuples: (start,stop) – to a simplified list of task, where the overlapping tasks merged together.

One exeption: my algorithm fails when one event is completely overlapped by another (s1 s2 e2 e1 )

Detailed:

• I've a list 'taskList' with pairs (lesson learned - tuples :).
• each tuple consists of 2 datetimeobjects: start and end of a task.
• important: the chronology of 'taskList' where the order is determined by start because tasks may overlapp
• 'taskList' consists several days, therefore datetime objects

Example, just string representation of time for readability

``````taskList = [(9:00,10:00),(9:30,11:00),(11:00,12:30),(13:30,14:00),(14:00,18:00)]
``````

final endresult :

``````result = [(9:00,12:30), (13:30,18:00)]
``````

now my thought was, when I rearrange the 'taskList' in the manner I questioned

``````taskListT1 = [(9:00,),(10:00,9:30),(11:00,11:00),(12:30,13:30),(14:00,14:00),(18:00,)]
``````

now I can eliminate those tuples (a,b) where a >= b:

``````taskListT2 = [(9:00,),(12:30,13:30),(18:00,)]
``````

and transform back:

``````result = [(9:00,12:30), (13:30,18:00)]
``````

This works, but it feels like something more Pythonic is out there:

``````l = [[1,2], [3,4], [5,6], [7,8]]
o = []
last = []
for a, b in l:
o.append(last [a])
last = [b]
o.append(last)

print o
``````

prints

``````[[1], [2, 3], [4, 5], [6, 7], [8]]
``````

This body also works:

``````o = [[l[0][0]]]
for i in range(len(l)-1):
o.append([l[i][1], l[i 1][0]])
o.append([l[-1][1]])
``````

``````l = [[1,2], [3,4], [5,6], [7,8]]
m = [([] if i==0 else [l[i-1][1]] ) ([] if i==len(l) else [l[i][0]]) for i in xrange(len(l) 1)]
``````

Do you mean:

``````pairs = [[1,2], [3,4], [5,6], [7,8]]
print pairs, '-``````
``` By : Tony Veijalainen ```
``` This video can help you solving your question :) By: admin ```
``` Related Questions Python: what is the difference between (1,2,3) and [1,2,3], and when should I use each? A Transpose/Unzip Function in Python (inverse of zip) Best way to check if a list is empty In Python, how can you easily retrieve sorted items from a dictionary? (adsbygoogle = window.adsbygoogle || []).push({}); How do I sort a list of dictionaries by values of the dictionary in Python? How do I split a string into a list? In Python, what is the fastest algorithm for removing duplicates from a list so that all elements are unique *while preserving order*? [duplicate] Is there a zip-like method in .Net? List all words in a dictionary that start with <user input> How can I join a list into a string (caveat)? Iterate a list with indexes in Python Clearing a list Removing a subset of a dict from within a list Python List vs. Array - when to use? Finding the index of an item given a list containing it in Python fast filter method in python How do I reverse a list using recursion in Python? Passing a list while retaining the original Given a list of variable names in Python, how do I a create a dictionary with the variable names as keys (to the variables' values)? Python list of lists, changes reflected across sublists unexpectedly ```
``` ```
``` About Us    Contact Us    Legal    feedback    Copyright © 2015 - All Rights Reserved - www.4answered.com 'undefined'=== typeof _trfq || (window._trfq = []);'undefined'=== typeof _trfd && (window._trfd=[]),_trfd.push({'tccl.baseHost':'secureserver.net'}),_trfd.push({'ap':'cpsh'},{'server':'p3plcpnl0831'}) // Monitoring performance to make your website faster. If you want to opt-out, please contact web hosting support.```