3

I'm trying to combine a list of first- and surnames with no luck. My code is like this at the moment:

firstname=['Tom','Dick','Steve']
surname=['Johnson','Smith','Doe']
for f in (firstname):
    for s in (surname):
        print(f,s)

Which gives me something like this:

Tom Johnson
Tom Smith
Tom Doe
Dick Johnson

And so on, when I really want:

Tom Johnson
Dick Smith
Steve Doe

Help much appreciated for a beginner like me.

Mats
  • 165
  • 1
  • 3
  • 14

4 Answers4

6
for name, surname in zip(firstname, surname):
   print(name, surname)
jsbueno
  • 99,910
  • 10
  • 151
  • 209
  • 1
    Thank you! This works wonderfully. Is it possible to iterate backwards as well using zip? – Mats Sep 22 '15 at 22:42
  • 1
    @Matsern Use `reverse` for reversing, but you need a sequence for that, whereas `zip` returns an iterator. So first consume the iterator into a list and then call `reverse`: `reversed(list(zip(firstname, surname)))`. – Thijs van Dien Sep 22 '15 at 22:48
  • You can use while loops which is too old fashioned but you can control (print) elements in list in different ways since you were talking about reverse. Thanks!! –  Sep 22 '15 at 22:59
  • 1
    @ThijsvanDien thank you for your answer, but I found that zip(name, reversed(surname)) worked best in my case. Just leaving this for posterity. – Mats Sep 22 '15 at 23:00
1

Zip is the way to combine two lists together in python. So, as a function,

def combine_names(first, last):
    comb = zip(first, last)
    for f,l in comb:
        print f,l
Shawn Mehan
  • 4,513
  • 9
  • 31
  • 51
0

Assuming both lists have the same length, you could iterate over it and use the corresponding index on both:

for i in range(len(firstname)):
    print (firstname[i], surname[i])
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0
firstname=['Tom','Dick','Steve']
surname=['Johnson','Smith','Doe']
i = 0
while i<min(len(firstname), len(surname)):
    print (firstname[i], surname[j])
    i += 1
Thijs van Dien
  • 6,516
  • 1
  • 29
  • 48
  • 1
    Why so terribly low level? – Thijs van Dien Sep 22 '15 at 22:49
  • @ThijsvanDien Yeah its very old fashioned, from his question, he is missing a fundamental concept of nested loop. And I'm guessing he is new to programming. So posted this classic and mechanical while loop :) –  Sep 22 '15 at 22:55
  • 1
    I appreciate your intentions, but I find this code rather confusing to be honest. The lists are equally long; (and even if they weren't) why use two loops at all? The outer loop in your example doesn't serve any purpose. – Thijs van Dien Sep 22 '15 at 23:00
  • Thanks for that, I have updated. I removed outer while, since the length of lists remain equal, I guess this wont be a problem. Is it too confusing? do you want me to delete this answer? –  Sep 22 '15 at 23:05
  • 1
    Since `i` and `j` are always equal, you can stick to just `i`. To account for possible differences in length, you might use `while i < min(len(firstname), len(surname))`. But then you might as well use a `for` loop. And then you might as well drop the indices completely... You see where this leads to. I don't mind different implementations, but really in this case it is unnecessary complication. I'm not sure how much that helps the OP. – Thijs van Dien Sep 22 '15 at 23:46