Home > Software design >  Python Question : How to update a list which contains customer info in python? The elements in the l
Python Question : How to update a list which contains customer info in python? The elements in the l

Time:07-07

Say for instance:

#audit of current year

name_of_members = ['julia','mike','stacey', 'alex']

is_team_member = ['yes','no','yes','no']

Basically, we can see that Julia and Stacey is a members of a team or they are part of a team whereas else Mike and Alex are not in any team.

Now, we have to check if in the next year these people are still team members or not and if they are in a team. basically, we have updated the data of each member.

so we might want to take input from the users that say

input("Is Julia still a team member?")

and the input given will be stored in the index where Julia's data of whether she is a team member or not was stored.

So I want to know how to do this.

CodePudding user response:

This should solve the issue:

for i in range(len(name_of_members)):
    is_team_member[i]= input("Is " name_of_members[i]  " still a team member?")

CodePudding user response:

Here's how you can do it

name_of_members = ['julia','mike','stacey', 'alex']
is_team_member = ['yes','no','yes','no']

for i in range(len(name_of_members)):
    if is_team_member[i] == 'yes':
        res = input(f'Is {name_of_members[i]} still a team member? ')
        if res == 'no':
            is_team_member[i] = 'no'

print(is_team_member)

I suggest using dictionary instead of two separate lists, Where key represents the member's name while the value represents if they are in a team or not.

members = {
    'julia': 'yes',
    'mike': 'no',
    'stacey': 'yes',
    'alex': 'no'
}

for name, response in members.items():
    if response == 'yes':
        res = input(f'Is {name} still a team member? ')
        if res == 'no':
            members[name] = 'no'


print(members)

CodePudding user response:

name_of_members = ['julia','mike','stacey', 'alex']

is_team_member = ['yes','no','yes','no']

for i in range(len(name_of_members)):
      data=input(f'{name_of_members[i]} Is still a team member? ')
      is_team_member[i]=data
#using list comprehension
is_team_member=[input(f'{name_of_members[i]} Is still a team member? ')for i in range(len(name_of_members))]
print(is_team_member)

CodePudding user response:

Convert your lists into one dictionary and then replace values in dictionary with responses from input

name_of_members = ['julia','mike','stacey', 'alex']

is_team_member = ['yes','no','yes','no']

name_dict={}
#Convert to dictionary
for x in range(len(name_of_members)):
    name_dict[name_of_members[x]]=is_team_member[x]

#Replace answers based on input
for k,v in name_dict.items():
    team_q = input(f"Is {k} still a team member? Yes or No?")
    name_dict[k] = team_q
print(name_dict)
  • Related