-1

I have a list like this

list=['a, b, c, d', 'e, f, g, h','i, j, k, l']

But i want to have a list like below way

target_list=[
              ['a','b','c','d']
              ['e','f','g','h']
              ['i','j','k','l']
           ]

Please help me to solve this. I am new into python.

snakecharmerb
  • 47,570
  • 11
  • 100
  • 153

2 Answers2

1

Based on the mentioned example, the length of your list is 3.

list[0] = 'a, b, c, d'
list[1] = 'e, f, g, h'
list[2] = 'i, j, k, l'

Refer to below code to get the desired target list:

list=['a, b, c, d','e, f, g, h','i, j, k, l']
#isalpha() method is used to catch only alphabet and omits spaces and commas
target_list = [[i for i in item if i.isalpha()] for item in list]
print(target_list)
Pooria_T
  • 136
  • 1
  • 7
0
list=['a','b','c','d','e','f','g','h','i','j','k','l']
target_list=[]
a=list[0:4]
b=list[4:8]
c=list[8:] 
target_list.append(a)
target_list.append(b)
target_list.append(c)
print(target_list)
  • 2
    Your starting list is not the same as the list in the question... – snakecharmerb Aug 16 '20 at 13:56
  • 3
    Also quality answers generally benefit from an explanation that can help future visitors apply your insight to their own code, increasing long term value and ears of understanding. – SherylHohman Aug 16 '20 at 16:39