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?

Ma0
  • 15,057
  • 4
  • 35
  • 65
kalpesh
  • 11
  • 1
  • 2
  • 1
    Welcome to Stack Overflow! Please read [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and edit your question thereafter – bork Jan 11 '19 at 08:40
  • I suggest you read this SO page on how to ask a good question and then edit this accordingly: https://stackoverflow.com/help/how-to-ask – Thirst for Knowledge Jan 11 '19 at 08:41

3 Answers3

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']]
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
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']]
Ma0
  • 15,057
  • 4
  • 35
  • 65
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']]