Reputation: 149
I am using python 3.x, I have the following problem, using the keyboard the user enters certain data and fills N lists to create a contact list, then in a list I collect all the data of the lists, I need to modify the data of each list, (I already have it, I modify the data of a list with a specific value using a for) Example, Names list, I modify Andrew's name, but in the Contacts list, there is all Andrew's information (phone, mail, etc), but I just need to modify in the Contacts list, the value of Andrew I have all this list:
names = []
surnames = []
phones = []
emails = []
addresses = []
ages = []
salaries = []
genres = []
contacts = []
# and use the append to add the data into the contacts list
contacts.append ([names, surnames, phone numbers, emails, addresses, ages, salaries, genders])
Then I update the info of one contact
search = input(Fore.LIGHTBLUE_EX + "Type the name of the contact you want update: ")
for i in range(len(names)):
if (names[i] == search):
try:
names[i] = input(Fore.MAGENTA + "Type the New name: ")
names[i] = nombres[i].replace(" ", "")
if names[i].isalpha() == True:
print(Fore.GREEN + "Already saved, congrats.")
pause= input(Fore.LIGHTGREEN_EX + "Press enter to exit")
But I dont know how to update the name in the List of contacts.
Upvotes: 1
Views: 287
Reputation: 4670
When you call contacts.append()
, you add a list of lists to a list, so your contacts
list will look something like this:
contacts = [[[names[0], names[1], ...], [...], [...]]]
It's unnecessary to have a list of one item nested in another list, so I would just call contacts.append()
and pass each list (names
, surnames
, etc.) to the method, which allows for easier indexing.
Since the list names
would be the first item in the list contacts
(contacts[0]
), you could do one of two things (there may be more, but these are off the top of my head):
contacts[0][0] = "updated name"
would update the first item of the names
list to "update name"
)contacts[0] = new_name_list
would reassign contacts[0]
, formerly the names
list, to new_name_list
)On a side note: In this case, I would recommend dictionaries over lists, as it will be easier to keep track of what is being reassigned/modified.
contacts = {
"names": names,
"surnames": surnames,
...
}
Doing this will make it more clear which list your are referring to; contacts[0]
doesn't give much information, but contacts["names"]
informs readers that you are referring to the names
list. This is solely for cleaner code; there isn't much difference in functionality.
Upvotes: 1