1

I'm trying to write a function that takes 2 list variables i.e. first_names and last_names.

I'm using a nested for loop in my function to iterate through both lists and append the values to return a new 'combined' variable list.

The function takes the two list parameters but iterates through just the first index value [0] of each list and outputs that - the loop ends.

first_names = ["Dave", "James", "Steve"]

last_names = ["Smith", "Jones", "Jackson"]

def NameCombine(first_names,last_names):
    combined = []
        for first in first_names:
            for last in last_names:
                combined.append(first+last)
                return combined

print(NameCombine(first_names,last_names))

Expected output: DaveSmith, JamesJones, SteveJackson

Actual output: DaveSmith

I'm expecting a new combined list of both the first and last name at each index.

But it's returning the first two values of each list and then the loop ends.

DJoseph1
  • 13
  • 4
  • 1
    Think about where you are returning from the function. How many elements have you iterated over at that point? – molbdnilo Jul 04 '19 at 13:04
  • Possible duplicate of [How to concatenate element-wise two lists in Python?](https://stackoverflow.com/questions/19560044/how-to-concatenate-element-wise-two-lists-in-python) – Georgy Jul 04 '19 at 13:04
  • Apologies all - left the while condition in the when i was experimenting and forgot to delete before copying over. Edited and corrected now. – DJoseph1 Jul 04 '19 at 13:06

4 Answers4

2

You can combine them with zip within comprehension:

def NameCombine(first_names,last_names):
    return [a+b for a, b in zip(first_names, last_names)]
zipa
  • 27,316
  • 6
  • 40
  • 58
  • This achieves the target output and props to @Pasan Chamikara for his answer which includes formatting too. I'm still trying to figure out if my original code can achieve the same output using a nested loop. – DJoseph1 Jul 04 '19 at 13:39
  • You got the logic right but the indentation was missing. Although the answer from @Pasan will work, but what this solution is more elegant and pythonic way to achieve the same result in a single line of code – Varun Verma Jul 04 '19 at 16:06
1

Since most of them here answer specifically to your question. I would like to add the other way of achieving the solution to this problem as below,

here I used map and add from operator module

list(map(operator.add, first_names, last_names))
# ['DaveSmith', 'JamesJones', 'SteveJackson']
0

You may try with this code.

first_names = ["Dave", "James", "Steve"]

last_names = ["Smith", "Jones", "Jackson"]

def NameCombine(first_names, last_names):
    combined = []
    for i in range(0, len(first_names)):
        if last_names[i] != None:
            combined.append(first_names[i] + " " + last_names[i])
        else:
            combined.append(first_names[i])
    return combined

print(NameCombine(first_names,last_names))
Pasan Chamikara
  • 715
  • 9
  • 21
  • Thanks @Pasan Chamikara. While this does achieve my target output, I'm still trying to figure out how to achieve the same output using a nested loop. – DJoseph1 Jul 04 '19 at 13:20
  • @DJoseph1 - A loop cycling three times nested in a loop also cycling three times yields a total of nine cycles, i. e. nine resulting values, while you only want three resulting values. Therefore, a nested loop cannot achieve what you want. – Armali Jul 04 '19 at 13:26
0

The pythonic and list comprehension is my preferred way. But if you really wanted a solution using a nested loop, this would work for you. Just used your code and added an additional condition to only print the desired result.

The results from this solution are: ['DaveSmith', 'JamesJones', 'SteveJackson']

first_names = ["Dave", "James", "Steve"]

last_names = ["Smith", "Jones", "Jackson"]

def NameCombine(first_names,last_names):
  combined = []
  for first in first_names:
    for last in last_names:
      if first_names.index(first) == last_names.index(last):
        combined.append(first+last)
  return combined

print(NameCombine(first_names,last_names))
Varun Verma
  • 704
  • 2
  • 10
  • 20