Raaydk
Raaydk

Reputation: 147

How to merge lists together?

I want to merged a list together with another list with the same length. My first list contains names for movie + actor names. The other list contains ratings based on the movie.

example from list with names + actors:

'The Godfather (1972), Marlon Brando, Al Pacino, James Caan'\n",
'The Godfather: Part II (1974), Al Pacino, Robert De Niro, Robert Duvall',\n",
'The Dark Knight (2008), Christian Bale, Heath Ledger, Aaron Eckhart',\n",
'Pulp Fiction (1994), John Travolta, Uma Thurman, Samuel L. Jackson',\n",

example from list with ratings:

'9.0',
'8.9',
'8.9',
'8.9',

I want to merged these two list together to one big list with

Names, actors, ratings.

The result should look like this:

'The Godfather (1972), Marlon Brando, Al Pacino, James Caan, 9.0'\n",
'The Godfather: Part II (1974), Al Pacino, Robert De Niro, Robert Duvall, 8.9',\n",
'The Dark Knight (2008), Christian Bale, Heath Ledger, Aaron Eckhart, 8.9',\n",
'Pulp Fiction (1994), John Travolta, Uma Thurman, Samuel L. Jackson, 8.9',\n",

tried this so far, but it did not help me alot.

from pprint import pprint

Name_Actor_List = []
Final_List = []



for i in lines:
    Name_Actor_List.append(i)

Final_List = Machine_List + ratings    

Upvotes: 0

Views: 114

Answers (3)

Oyvindkg
Oyvindkg

Reputation: 111

This line should do the trick by using map() and a simple lambda-function:

result = map( lambda i: actor_list[i] + ratings[i], range(len(actor_list)) )

Upvotes: 0

Michel Keijzers
Michel Keijzers

Reputation: 15357

Something like this should also work:

result = []
for index in len(actor_list):
    result.add('%s,%s', actor_list[index], ratings[index])

Upvotes: 0

jonrsharpe
jonrsharpe

Reputation: 121966

The easiest way to combine items from two lists at the same index is zip:

Final_List = []
for name_actor, rating in zip(lines, ratings):
    Final_List.append(",".join(name_actor, rating))

Upvotes: 1

Related Questions