0
How can I add the items of inner tuples and inner lists to the outer list. I have done this but how do I assign it
Is there any simple way than this ... This has complexity of O(nsquare) list1= [(1,2,3),[1,2],['a','hit','less']] for each in list1 : # print (each ) for item in each: print (item)
7 ответов
+ 2
Sevda Krithika Sharma
Use nested for loops or a comprehension.
new = [ ]
for x in list1:
for y in x:
new.append(y)
print(new)
Or...
list1_new = [j for i in list1 for j in i]
print(list1_new)
+ 1
list1= [(1,2,3),[1,2],['a','hit','less']]
L =[]
for each in list1 :
L += each
print(L)
Single for loop. Is this OK?
+ 1
Jayakrishna🇮🇳 Tomiwa Joseph Thank you
0
How can the time complexity be O(n²)? You are iterating once through the array. It has complexity O(n).. I think it is the best way..
0
For within for ...so it will be have time complexity of O(n square)Arnesh
0
Arnesh but how do I remove the inner tuples and lists and add individuals to the same list1 back
0
Sevda Krithika Sharma you're welcome..