-3

If I had a list

['xxxx', 'oooo', 'xxxx', 'oooo'......etc]

which looks like

                                                (xxxx)

                                                (oooo)

                                                (xxxx)

                                                (oooo)

and the list could be as long as the user inputs,

how would I make a new list sorted by each column which would look like:

['xoxo', 'xoxo', 'xoxo', 'xoxo']

which would be

           (xoxo)

           (xoxo)

           (xoxo)

           (xoxo)
3
  • Have you tried anything? See how to ask
    – aIKid
    Oct 12, 2013 at 3:58
  • So you essentially want to transpose a 2D array?
    – fjarri
    Oct 12, 2013 at 3:59
  • 1
    You start out by saying this is a list of strings, but then parentheses appear (indicating tuples?) and I end up having no idea what you want. What does sorting have to do with anything? What do columns have to do with anything?
    – roippi
    Oct 12, 2013 at 4:01

1 Answer 1

2
myList=['xxxx', 'oooo', 'xxxx', 'oooo']
print [''.join(element) for element in zip(*myList)]

Output

['xoxo', 'xoxo', 'xoxo', 'xoxo']

What you are looking for, is called, transposing an array. That can be achieved with zip function.

1
  • Please use a different variable name.
    – roippi
    Oct 12, 2013 at 4:04

Not the answer you're looking for? Browse other questions tagged or ask your own question.