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)

28th Jul 2020, 4:47 PM
Sevda Krithika Sharma
Sevda Krithika Sharma - avatar
7 Réponses
+ 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)
28th Jul 2020, 6:21 PM
Tomiwa Joseph
Tomiwa Joseph - avatar
+ 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?
28th Jul 2020, 6:20 PM
Jayakrishna 🇮🇳
28th Jul 2020, 6:30 PM
Sevda Krithika Sharma
Sevda Krithika Sharma - avatar
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..
28th Jul 2020, 4:55 PM
Arnesh
Arnesh - avatar
0
For within for ...so it will be have time complexity of O(n square)Arnesh
28th Jul 2020, 4:57 PM
Sevda Krithika Sharma
Sevda Krithika Sharma - avatar
0
Arnesh but how do I remove the inner tuples and lists and add individuals to the same list1 back
28th Jul 2020, 4:59 PM
Sevda Krithika Sharma
Sevda Krithika Sharma - avatar
0
Sevda Krithika Sharma you're welcome..
29th Jul 2020, 9:57 AM
Jayakrishna 🇮🇳