home
Learn
screen_rotation
# create a new list using loop mylist = [[10,20,30,40,50],[100,200,300,400,500],[1000,2000,3000,4000,5000]] newlist = [] for innerlist in mylist: for item in innerlist: newlist.append(item) print("using for loop:",newlist) # create a new list using comprehension mylist = [[10,20,30,40,50],[100,200,300,400,500],[1000,2000,3000,4000,5000]] newlist = [item for innerlist in mylist for item in innerlist] print("using list comprehension:",newlist)
Program Output
using for loop: [10, 20, 30, 40, 50, 100, 200, 300, 400, 500, 1000, 2000, 3000, 4000, 5000] using list comprehension: [10, 20, 30, 40, 50, 100, 200, 300, 400, 500, 1000, 2000, 3000, 4000, 5000]