0

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    
Tim
  • 41,901
  • 18
  • 127
  • 145
Raaydk
  • 147
  • 2
  • 12
  • possible duplicate of [Merge two lists in Python?](http://stackoverflow.com/questions/1720421/merge-two-lists-in-python) – thefourtheye May 12 '14 at 10:57
  • @thefourtheye - This is not a duplicate of the one you linked. The linked question is about appending a list to another list. This question is about combining each element from one list with a corresponding element from another list. – Rynant May 12 '14 at 15:18
  • If the newlines in the list elements are there from a `file.readlines()`, it would be better to read the files with `file.read().splitlines()`. This way the strings won't contain newlines, and combining the strings will be easier. – Rynant May 12 '14 at 15:24

3 Answers3

1

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))
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
0

Something like this should also work:

result = []
for index in len(actor_list):
    result.add('%s,%s', actor_list[index], ratings[index])
Michel Keijzers
  • 15,025
  • 28
  • 93
  • 119
0

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)) )

Oyvindkg
  • 111
  • 10