-5

The following code will take a list of names and a list of greetings and combine them so one greeting will be applied with one name in the order of their index. Such as we say “hi susan”, “hola juan”, and so forth.

1.) How do I revise the following code so that I can store those in the greetings list for a future greeting for that individual and print the combined greeting list?

# Implemented as a While Loop.
names_list = ['susan', 'juan', 'wolfgang', 'piere', 'aednat']
greetings_list = ['hi', 'hola', 'tag', 'salut', 'haigh']
idx = 0
while idx < len(names_list):
    greetings_list[idx] = greetings_list[idx] + " " + names_list[idx]
    idx = idx + 1
print(greetings_list)

2.) How do I rewrite this code to use a for loop instead of the while loop?

Dirk
  • 1
  • 1
  • Possible duplicate of [How to iterate through two lists in parallel?](https://stackoverflow.com/questions/1663807/how-to-iterate-through-two-lists-in-parallel) – bgfvdu3w Oct 03 '17 at 05:49
  • SO is not a code writing service. You should show us what you have tried tovsolve the problems so far. – Klaus D. Oct 03 '17 at 07:40

2 Answers2

0

Use zip to iterate over both lists simultaneously (provided the lists are of the same length and items of the same index are related - as in this case):

names_list = ['susan', 'juan', 'wolfgang', 'piere', 'aednat']
greetings_list = ['hi', 'hola', 'tag', 'salut', 'haigh']

for greeting, name in zip(greetings_list, names_list):
    print("%s %s" % (greeting, name))
jrd1
  • 10,358
  • 4
  • 34
  • 51
  • Ok so it would look like this?: names_list = ['susan', 'juan', 'wolfgang', 'piere', 'aednat'] greetings_list = ['hi', 'hola', 'tag', 'salut', 'haigh'] for name, greeting in zip(names_list, greetings_list): print("%s %s" % (name, greeting)) print(greetings_list) –  Dirk Oct 03 '17 at 06:03
  • Because the output I am getting is susan hi juan hola wolfgang tag piere salut aednat haigh ['hi', 'hola', 'tag', 'salut', 'haigh'] –  Dirk Oct 03 '17 at 06:04
  • @Dirk, I believe so - although, I'm curious: why are you outputting `greetings_list`? Given your use of it in the first comment, it seems like you want to update that list - is that correct? – jrd1 Oct 03 '17 at 06:06
  • I want an output saying “hi susan”, “hola juan”, and so forth. But I'm not getting that output. –  Dirk Oct 03 '17 at 06:15
  • @Dirk: Ah, I see! Edited. The above code should provide the result you seek. – jrd1 Oct 03 '17 at 06:18
  • To clarify, `zip` creates an aggregate iterator from several iterable objects - i.e. it's not limited to 2 lists, one can use many if need be. – jrd1 Oct 03 '17 at 06:23
  • Thank you! That's very close to the desired solution, but it should be in a list form. Right now it's outputting like this: hi susan hola juan tag wolfgang salut piere haigh aednat Instead I want it to be like hi Susan, hello Juan, etc. –  Dirk Oct 03 '17 at 06:26
  • @Dirk: Hmm... Surely you can do this yourself, yes? It's not too difficult, all things considered. From what you've commented, it appears you understand very little Python at all. While there's nothing wrong with that since we all were beginners once, for me to continue on in such manner would rob you of the incredibly rewarding experience of learning and problem-solving. Otherwise, as-is, we're essentially doing what seems to be homework for you. (Not that I'm saying that you are doing this, but unless you can show otherwise from your own attempts, this is the extent of what I'll do.) – jrd1 Oct 03 '17 at 06:31
  • Ok, would I use the dictionary function to create a list? –  Dirk Oct 03 '17 at 06:35
  • I appreciate if you would help me, and in the process I am learning. –  Dirk Oct 03 '17 at 06:37
  • @Dirk: If you used a dictionary function, you'll create a dictionary - that's not what you want. You want a list, so use a list - hint: you've created a couple in your first program. – jrd1 Oct 03 '17 at 06:38
  • Ok so would I use greetings_list = greetings_list+ " " + names_list –  Dirk Oct 03 '17 at 06:43
  • @Dirk: No, that will never work as they are incompatible types. For more help, please see: https://www.programiz.com/python-programming/list – jrd1 Oct 03 '17 at 06:46
0

You can store combined list by zipping the two list contents in a new list .

combined_list = [ [greeting, name] for name,greeting in zip(names_list,greetings_list)]

Then , as needed you can iterate through it

>>> for i in combined_list:
    ' '.join(i)

'hi susan'
'hola juan'
'tag wolfgang'
'salut piere'
'haigh aednat'
Anil_M
  • 10,893
  • 6
  • 47
  • 74
  • Hi, the output you gave me comes close to the desired solution. Thank you! But it's printing out like this: [['hi', 'susan'], ['hola', 'juan'], ['tag', 'wolfgang'], ['salut', 'piere'], ['haigh', 'aednat']] Is there a way to get rid of the brackets? –  Dirk Oct 03 '17 at 06:16
  • Here is my code that I put in: names_list = ['susan', 'juan', 'wolfgang', 'piere', 'aednat'] greetings_list = ['hi', 'hola', 'tag', 'salut', 'haigh'] idx = 0 combined_list = [ [greeting, name] for name,greeting in zip(names_list,greetings_list)] for i in combined_list: ' '.join(i) print(combined_list) –  Dirk Oct 03 '17 at 06:18
  • you are printing combined list. print this: for i in combined_list: print ' '.join(i) – Anil_M Oct 03 '17 at 06:20
  • Did that solved the problem? If yes then do you mind accepting the answer to close the loop. – Anil_M Oct 03 '17 at 13:04