Reputation: 661
My list:
>>> l = [["A", "A1", 1, 2, 3],
["B", "A2", 4, 5, 6],
["C", "A3", 7, 8, 9],
["D", "A4", 10, 11, 12]]
Slicing operation:
>>> [[n[0] for n in l], [u[1:] for u in l]]
[['A', 'B', 'C', 'D'],
[['A1', 1, 2, 3],
['A2', 4, 5, 6],
['A3', 7, 8, 9],
['A4', 10, 11, 12]]]
Is any way to slice this list without extra square brackets? like below:
[['A', 'B', 'C', 'D'],
['A1', 1, 2, 3],
['A2', 4, 5, 6],
['A3', 7, 8, 9],
['A4', 10, 11, 12]]
Upvotes: 0
Views: 2300
Reputation: 239
Like this?
>>> mylist = [[n[0] for n in l]] + [u[1:] for u in l]
>>> mylist
[['A', 'B', 'C', 'D'], ['A1', 1, 2, 3], ['A2', 4, 5, 6], ['A3', 7, 8, 9], ['A4', 10, 11, 12]]
Upvotes: 1
Reputation: 239443
You can simply create another list and join the result like this
print [[n[0] for n in l]] + [u[1:] for u in l]
Upvotes: 2
Reputation: 122336
You can concatenate the lists:
>>> [[n[0] for n in l]] + [u[1:] for u in l]
[['A', 'B', 'C', 'D'], ['A1', 1, 2, 3], ['A2', 4, 5, 6], ['A3', 7, 8, 9], ['A4', 10, 11, 12]]
Upvotes: 3