Home > Software engineering >  Iterate through a list to get nicknames
Iterate through a list to get nicknames

Time:08-26

I'm trying to make an email generator using a list of names and their common nicknames. How would I check to see if the first name is in the name list and if it is iterate through a list of nick names for the name.

Code:

name_list = ["Aaron", "Abel", "Abiah", "Abijah", "Abiel"]

name = "Aaron"
Aaron = ["Erin", "Ron", "Ronnie"]

i = 0

if name in name_list:
    print("Yes")
    nick_name_length = len(name)
    while nick_name_length >= i:
        print(name[i])
        i  = 1

I am having difficulty switching between the name variable and the list.

Desired Output:

Erin
Ron
Ronnie

CodePudding user response:

Don't create variables based on the name. Instead create a nicknames dictionary.

nicknamesbyname = {
    "Aaron": ["Erin", "Ron", "Ronnie"],
    "Steven": ["Stevie", "Steve"]
}

To get the nicknames for any given name just do

nicknames = nicknamesbyname.get(name, [])

If there are no nicknames you get back an empty list.

Note that you could just put all of your names as keys in the nicknamesbyname and dispense with the names list altogether.

nicknamesbyname = {
    "Aaron": ["Erin", "Ron", "Ronnie"],
    "Abel": ["Abe"],
    "Abiah": [], # no nicknames so it's just an empty list
    "Steven": ["Stevie", "Steve"]
}

CodePudding user response:

You're almost there, you don't need to have the i iterator Try this:

name_list = ["Aaron", "Abel", "Abiah", "Abijah", "Abiel"]

name = "Aaron"
Aaron = ["Erin", "Ron", "Ronnie"]
if name in name_list:
    print("Yes")
    for nickname in Aaron:
        print(nickname)

If you wanted to do it your way using i, you reference the wrong list:

name_list = ["Aaron", "Abel", "Abiah", "Abijah", "Abiel"]
name = "Aaron"
Aaron = ["Erin", "Ron", "Ronnie"]
i = 0
if name in name_list:
    print("Yes")
    nick_name_length = len(Aaron)
    while nick_name_length >= i:
        print(Aaron[i])
        i  = 1

CodePudding user response:

A couple of small changes will make it do what you want:

name_list = ["Aaron", "Abel", "Abiah", "Abijah", "Abiel"]

name = "Aaron"
Aaron = ["Erin", "Ron", "Ronnie"]

i = 0

if name in name_list:
    print("Yes")
    nick_name_length = len(Aaron)  ### 'name' changed to 'Aaron'
    while nick_name_length > i:    ### '>=' changed to '>'
        print(Aaron[i])            ### 'name' changed to 'Aaron'
        i  = 1

And this is a bit more elegant

name_list = ["Aaron", "Abel", "Abiah", "Abijah", "Abiel"]
name = "Aaron"
Aaron = ["Erin", "Ron", "Ronnie"]

if name in name_list:
    print("Yes")
    for i in range(len(Aaron)): 
        print(Aaron[i])  

And this is one approach to making it more actionable, storing the nicknames in a dictionary

name_list = ["Aaron", "Abel", "Abiah", "Abijah", "Abiel"]
nicknames = {"Aaron":["Erin", "Ron", "Ronnie"],
             "Abel":["Abe"]
            }
for name in name_list:
    nicks = nicknames.get(name,[])
    for nick in nicks: 
        print(name, nick)   

CodePudding user response:

Try this code:

nicknames_dict = {
    "Aaron" :["Erin", "Ron", "Ronnie"],
    "Abel": [],
    "Steven" :["Stevie", "Steve"]
}
name_list = ["Aaron", "Abel", "Abiah", "Abijah", "Abiel", "Steven"]

mail_dict = {name: [f'{nickname.lower()}@yourmail.com' 
                for nickname in nicknames_dict.get(name, [])] 
         for name in name_list }

and mail_dict contains:

{'Aaron': ['[email protected]', '[email protected]', '[email protected]'],
 'Abel': [],
 'Abiah': [],
 'Abijah': [],
 'Abiel': [],
 'Steven': ['[email protected]', '[email protected]']}

nicknames_dict is a dictionary that holds <key, value list> pairs where the key is the name and the value list is the list of nicknames <name, nicknames_list>.

list_name is the list of names whose emails you want to create.

dict_mail is the dictionary containing <name, mail_list>.

  • Related