-2

The following code is supposed to take two lists of strings, the lists having the same length, and concatenate corresponding entries in the list. For example, it should take the first entries from each list, "Homer" and "Simpson", and concatenate them to produce "Homer Simpson", and so on for the next entries.

first_name = ['Homer', 'Marge', 'Bart', 'Lisa']
last_name = ['Simpson', 'Carlson', 'Flanders', 'Gamble']

for i in range(len(first_name)):
    for n in range(len(last_name)):
        print(first_name[i] + " " + last_name[n])

Expected Result:

Homer Simpson
Marge Carlson
Bart Flanders
Lisa Gamble

Actual Result:

Homer Simpson
Homer Carlson
Homer Flanders
Homer Gamble
Marge Simpson
Marge Carlson
Marge Flanders
Marge Gamble
Bart Simpson
Bart Carlson
Bart Flanders
Bart Gamble
Lisa Simpson
Lisa Carlson
Lisa Flanders
Lisa Gamble
milanbalazs
  • 4,811
  • 4
  • 23
  • 45
nogils
  • 1
  • 1
  • use `zip` to iterate over the lists in parallel: `print(",\n".join([" ".join([f, l]) for f, l in zip(first_name, last_name)]))` or if you prefer the `for` loop over the list comp: `for f, l in zip(first_name, last_name): print(f + " " + l)` – pault Aug 15 '19 at 17:25
  • Welcome to stackoverflow! Thank you for providing concise code and a good description of what your code is trying to achieve. – turbulencetoo Aug 15 '19 at 18:17

5 Answers5

2

Right now, you're creating every possible combination of the two lists. You might want the following.

first_name = ['Homer', 'Marge', 'Bart', 'Lisa']
last_name = ['Simpson', 'Carlson', 'Flanders', 'Gamble']

for i in range(0, len(first_name)):
    print(first_name[i] + " " + last_name[i])

Additionally, this program (and your previous program) can be done using itertools. What you've originally done is itertools.product, but what you want to do is itertools.combinations.

Another way to program what you wanted, is by using itertools.combinations.

import itertools.combinations as combos

first_name = ['Homer ', 'Marge ', 'Bart ', 'Lisa ']
last_name = ['Simpson', 'Carlson', 'Flanders', 'Gamble']

print(itertools.combinations(first_name, last_name)
ds_secret
  • 338
  • 3
  • 18
2

Simple list comprehension using zip

print(['{} {}'.format(first, last) for first, last in zip(first_name, last_name)])

Output:

['Homer Simpson', 'Marge Carlson', 'Bart Flanders', 'Lisa Gamble']
Adrian
  • 177
  • 13
0

Please try:

for i in range(len(first_name)):
    print(first_name[i] + " " + last_name[i])

In your code, since you have 2 loops, you are appending each first name with each last name

Michael Bianconi
  • 5,072
  • 1
  • 10
  • 25
0

To avoid loops, you can do:

print(*sum(zip(first_name, last_name), ()))
>>> Homer Simpson Marge Carlson Bart Flanders Lisa Gamble

Or you can use map:

 print(*map(lambda a,b:a+str(' ')+b, first_name, last_name))
>>> Homer Simpson Marge Carlson Bart Flanders Lisa Gamble
Sayandip Dutta
  • 15,602
  • 4
  • 23
  • 52
  • 1
    This does not avoid loops. It just hides the hoop. – pault Aug 15 '19 at 17:49
  • `sum` also does not avoid the loop, and using [`sum` to concatenate the tuples is an anti-pattern](https://stackoverflow.com/questions/41772054/why-sum-on-lists-is-sometimes-faster-than-itertools-chain). – pault Aug 15 '19 at 18:20
-1

You don't want to loop through last_name separately. Instead, you can use one loop like this:

for i in range(len(first_name)):
  print(first_name[i] + last_name[i])
Michael Bianconi
  • 5,072
  • 1
  • 10
  • 25