Combing two lists together with zip() function through list comprehension or plain function

17 Views Asked by At

I am new to this site. If anyone wants to combine two list I found the zip function works for both list comprehension and just as a function to combine both lists. The best explanation I heard for the zip() function is that it literally acts like a zipper by combining items from each list together, in a tuple, to bridge a gap. I spent most of the morning researching how to combine two lists and it was really rewarding. Since I've been helped through this site I figure I will put what I learned here for others to learn from. Feel free to improve if flaws found.(I was combining two lists after scraping from web)

See code below:

songs = ['We Found Love', 'Sexy And I Know It', 'It Will Rain', 'Moves Like Jagger', 'Good Feeling'] artists = ['Rihanna Featuring Calvin Harris', 'LMFAO', 'Bruno Mars', 'Maroon 5 Featuring Christina Aguilera', 'Flo Rida']

zipped = zip(songs,artists)

song_artists = list(zipped)

print(song_artists)

#-----this code works because you zip to lists together, creating tuples for each pair list item at the same index, then calling the list function to consume the iterator.

or

songs = ['We Found Love', 'Sexy And I Know It', 'It Will Rain', 'Moves Like Jagger', 'Good Feeling'] artists = ['Rihanna Featuring Calvin Harris', 'LMFAO', 'Bruno Mars', 'Maroon 5 Featuring Christina Aguilera', 'Flo Rida']

song_artists = [[x,y] for x,y in zip(song, artists)]

#-------this code works because you call the zip function within a list, song_artists, and then declare a new tuple [x,y] for each paired tuple, from the newly zipped list combing the each list item from song and artist.

or

songs = ['We Found Love', 'Sexy And I Know It', 'It Will Rain', 'Moves Like Jagger', 'Good Feeling'] artists = ['Rihanna Featuring Calvin Harris', 'LMFAO', 'Bruno Mars', 'Maroon 5 Featuring Christina Aguilera', 'Flo Rida']

zipped = zip(songs,artists)

song_artists = list(zipped)

song_artists = [[x,y] for x,y in song_artists]

print(song_artists)

#------this code pulls each tuple from the zipped list, song_artists, as a new tuple, [x,y], in the new list sog_artists, so basically it is just transposing the list in song_artists into the new list with the same name, as lists comprehension takes the n from one list as n in a new list, in this case x,y in the original list as x,y in a new list.

If anyone wants to combine two list I found the zip function works for both list comprehension and just as a function to combine both lists. The best explanation I heard for the zip() function is that it literally acts like a zipper by combining items from each list together, in a tuple, to bridge a gap. I spent most of the morning researching how to combine two lists and it was really rewarding. Since I've been helped through this site I figure I will put what I learned here for others to learn from. Feel free to improve if flaws found.(I was combining two lists after scraping from web)

See code below:

songs = ['We Found Love', 'Sexy And I Know It', 'It Will Rain', 'Moves Like Jagger', 'Good Feeling'] artists = ['Rihanna Featuring Calvin Harris', 'LMFAO', 'Bruno Mars', 'Maroon 5 Featuring Christina Aguilera', 'Flo Rida']

zipped = zip(songs,artists)

song_artists = list(zipped)

print(song_artists)

#-----this code works because you zip to lists together, creating tuples for each pair list item at the same index, then calling the list function to consume the iterator.

or

songs = ['We Found Love', 'Sexy And I Know It', 'It Will Rain', 'Moves Like Jagger', 'Good Feeling'] artists = ['Rihanna Featuring Calvin Harris', 'LMFAO', 'Bruno Mars', 'Maroon 5 Featuring Christina Aguilera', 'Flo Rida']

song_artists = [[x,y] for x,y in zip(song, artists)]

#-------this code works because you call the zip function within a list, song_artists, and then declare a new tuple [x,y] for each paired tuple, from the newly zipped list combing the each list item from song and artist.

or

songs = ['We Found Love', 'Sexy And I Know It', 'It Will Rain', 'Moves Like Jagger', 'Good Feeling'] artists = ['Rihanna Featuring Calvin Harris', 'LMFAO', 'Bruno Mars', 'Maroon 5 Featuring Christina Aguilera', 'Flo Rida']

zipped = zip(songs,artists)

song_artists = list(zipped)

song_artists = [[x,y] for x,y in song_artists]

print(song_artists)

#------this code pulls each tuple from the zipped list, song_artists, as a new tuple, [x,y], in the new list sog_artists, so basically it is just transposing the list in song_artists into the new list with the same name, as lists comprehension takes the n from one list as n in a new list, in this case x,y in the original list as x,y in a new list.

0

There are 0 best solutions below