0

I have a list m:

m = ['ABC', 'XYZ', 'LMN']

and I want output as follows:

m = [['a','x','l']
     ['b','y','m']
     ['c','z','n']]

How can this be done?

2

3 Answers 3

1

Use list(zip(*..)) to transpose the nested list, and use list comprehension to create nested list:

print(list(zip(*[list(i.lower()) for i in m])))

Output:

[('a', 'x', 'l'), ('b', 'y', 'm'), ('c', 'z', 'n')]

If want sub-values to be lists:

print(list(map(list,zip(*[list(i.lower()) for i in m]))))

Output:

[['a', 'x', 'l'], ['b', 'y', 'm'], ['c', 'z', 'n']]
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help
@kalpesh Don't forget to accept it if it works :-)
1

All you need is a list-comprehension, zip to transpose and map to convert to lower.

   > m=['ABC','XYZ','LMN']
   > [list(map(str.lower, sub)) for sub in zip(*m)]
   [['a', 'x', 'l'], ['b', 'y', 'm'], ['c', 'z', 'n']]

3 Comments

Not OP's desired output
@U9-Forward. Oops. Did not see the transpose. Thanks!
Lol, :-), happy you edited it.
0
m=['ABC','XYZ','LMN']
import numpy as np
new_list = [[0, 0, 0], [0, 0,0],[0,0,0]]

j = 0

for i in m:
    a = list(i.lower())
    print(a)
    new_list[j] = a
    j = j+1
np.transpose(new_list).tolist()

Output:

[['a', 'x', 'l'], ['b', 'y', 'm'], ['c', 'z', 'n']]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.