1

Is it possible to convert this:

[ [0], [1], [2], [3], [4], [5], [[6,9]], [7], [8], [[6,9]] ]

into this:

[ [0], [1], [2], [3], [4], [5], [6,9], [7], [8], [6,9] ]
1
  • 1
    I now suspect it's a XY problem, how do you get the first list? Commented Apr 13, 2014 at 3:59

3 Answers 3

2
[item if isinstance(item, list) else [item] for items in data for item in items]
# [[0], [1], [2], [3], [4], [5], [6, 9], [7], [8], [6, 9]]
Sign up to request clarification or add additional context in comments.

Comments

2

You can do:

x = [i[0] if (type(i[0])==list and len(i[0])>1) else i for i in a]

Example

>>> a = [ [0], [1], [2], [3], [4], [5], [[6,9]], [7], [8], [[6,9]] ]
>>> x = [i[0] if (type(i[0])==list and len(i[0])>1) else i for i in a]
>>> x
[[0], [1], [2], [3], [4], [5], [6, 9], [7], [8], [6, 9]]

Comments

0

You could do this:

>>> data = [ [0], [1], [2], [3], [4], [5], [[6,9]], [7], [8], [[6,9]] ]
>>> o_data = [a[0] if isinstance(a[0], list) else a for a in data]
>>> o_data
[[0], [1], [2], [3], [4], [5], [6, 9], [7], [8], [6, 9]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.