Home > front end >  How to create two lists/or more, with one list?
How to create two lists/or more, with one list?

Time:01-11

lst =["1","2","","3","4","5","","6"]

Given the list above, how do I split the list an create another list whenever I find ""(the empty quotation marks alone)?

lst =["1","2","","3","4","5","","6"]
lst2 = []
for a in lst:
    if a == "":
        continue
    lst2.append(int(a))


print(lst2)
Output:[1, 2, 3, 4, 5, 6]

Expected output: lst2 = [["1","2"],["3","4","5"],["6"]]

CodePudding user response:

lst =["1","2","","3","4","5","","6"]
arr = []
new_lst = []
for i in lst:
  if i == "":
     arr.append(new_lst)
     new_lst = []
  else:
      new_lst.append(i)
 

if len(new_lst) > 0:
    arr.append(new_lst)


print(arr)
  • Related