0

I have the following list (which is much larger in reality)

[(1, 2, 3),
 (1, 2, 3),
 (1, 2, 3)]

and would like to insert 2 columns to it so it results in

 [(c1, 1, c2, 2, 3),
  (c1, 1, c2, 2, 3),
  (c1, 1, c2, 2, 3)]

the best which I came up with is the following

mylist = [(1, 2, 3),(1, 2, 3),(1, 2, 3)]
mylist_new = []
for i in mylist:
    mylist_new.append(('c1', i[0], 'c2', i[1], i[2], i[3]))

any alternative suggestions on how to achieve this?

1
  • Those elements aren't columns. They are tuples. Commented Mar 28 at 20:03

1 Answer 1

4

You could use list comprehension.

mylist = [(1, 2, 3),(1, 2, 3),(1, 2, 3)]
mylist_new = [('c1', i, 'c2', j, k) for (i, j, k) in mylist]
Sign up to request clarification or add additional context in comments.

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.