Python Programming - to modify a list item in a dictionary
Clash Royale CLAN TAG#URR8PPP
Python Programming - to modify a list item in a dictionary
Question: # Write a program to modify the email addresses in the records
dictionary to reflect this change
records
records = 57394: ['Suresh Datta', 'suresh@example.com'], 48539: ['ColetteBrowning', 'colette@example.com'], 58302: ['Skye Homsi','skye@example.com'], 48502: ['Hiroto Yamaguchi', 'hiroto@example.com'], 48291: ['Tobias Ledford', 'tobias@example.com'], 48293: ['Jin Xu', 'jin@example.com'], 23945: ['Joana Dias', 'joana@example.com'], 85823: ['Alton Derosa', 'alton@example.com']
I have iterated through the dictionary and created a new list with the values and split the email at @ and was able to change the the email from .com to .org.
My approach was to join the changed email and change the values of the dictionary. However, I keep on getting a TypeError: sequence item 0: expected str instance, list found
my code :
lst2 =
for value in records.values():
lst2.append(value[1].split('@'))
for items in lst2:
items[1] = 'examples.org'
for items in lst2:
','.join(lst2)
Ok, then how about my answer
– U9-Forward
Aug 12 at 3:27
problem in last line, you cannot use
','.join(lst2)
, lst2
is two-dimensional array– pwxcoo
Aug 12 at 3:29
','.join(lst2)
lst2
Appreciate your help. I understand the mistake I've made. Your answer worked as well.
– KKH
Aug 12 at 3:37
3 Answers
3
The issue is in your final for loop:
for items in lst2:
','.join(lst2)
You are joining should be joining items not lst2. However if you fix that it still won't work. You need to create a third list and add the values to it like this:
lst3 =
for items in lst2:
lst3.append('@'.join(items))
Then, lst3 will have the properly formatted emails.
It worked perfectly.
– KKH
Aug 12 at 3:33
@KKH Glad I could help! If you've solved your problem, click the green check on my answer to accept it.
– The Tesseract's Shadow
Aug 12 at 3:34
You can do a one-liner list-comprehension for, then iterate and do join the split of i
with ','
, so try this:
i
','
print([','.join(i[1].replace('.com','.org').split('@')) for i in records.values()])
Output:
['suresh,example.org', 'colette,example.org', 'skye,example.org', 'hiroto,example.org', 'tobias,example.org', 'jin,example.org', 'joana,example.org', 'alton,example.org']
Or:
print(['@'.join(i[1].replace('.com','.org').split('@')) for i in records.values()])
Output:
['suresh@example.org', 'colette@example.org', 'skye@example.org', 'hiroto@example.org', 'tobias@example.org', 'jin@example.org', 'joana@example.org', 'alton@example.org']
Or if want to edit dict:
print(k:[i.replace('.com','.org') for i in v] for k,v in records.items())
Output:
57394: ['Suresh Datta', 'suresh@example.org'], 48539: ['ColetteBrowning', 'colette@example.org'], 58302: ['Skye Homsi', 'skye@example.org'], 48502: ['Hiroto Yamaguchi', 'hiroto@example.org'], 48291: ['Tobias Ledford', 'tobias@example.org'], 48293: ['Jin Xu', 'jin@example.org'], 23945: ['Joana Dias', 'joana@example.org'], 85823: ['Alton Derosa', 'alton@example.org']
@KKH If my answer works, please accept it
– U9-Forward
Aug 12 at 3:28
It didn't work.
– KKH
Aug 12 at 3:30
@KKH Ok, can you please tell me what you want, and the desired output?
– U9-Forward
Aug 12 at 3:30
@U9-Forward Really awesome one line, but it doesn't help the asker learn what they did wrong. You don't explain what their error is, you just wrote the whole program for them. Really impressive, but not super helpful to the asker.
– The Tesseract's Shadow
Aug 12 at 3:33
@TheTesseract'sShadow When you posted the comment that was when i was editing my answer
– U9-Forward
Aug 12 at 3:34
Try the following code
records = 57394: ['Suresh Datta', 'suresh@example.com'], 48539: ['ColetteBrowning', 'colette@example.com'], 58302: ['Skye Homsi','skye@example.com'], 48502: ['Hiroto Yamaguchi', 'hiroto@example.com'], 48291: ['Tobias Ledford', 'tobias@example.com'], 48293: ['Jin Xu', 'jin@example.com'], 23945: ['Joana Dias', 'joana@example.com'], 85823: ['Alton Derosa', 'alton@example.com']
for key, value in records.items():
new_data =
for data in value:
new_data.append(data.replace('.com', '.org'))
records[key] = new_data
print(records)
57394: ['Suresh Datta', 'suresh@example.org'], 48539: ['ColetteBrowning', 'colette@example.org'], 58302: ['Skye Homsi', 'skye@example.org'], 48502: ['Hiroto Yamaguchi', 'hiroto@example.org'], 48291: ['Tobias Ledford', 'tobias@example.org'], 48293: ['Jin Xu', 'jin@example.org'], 23945: ['Joana Dias', 'joana@example.org'], 85823: ['Alton Derosa', 'alton@example.org']
I wasn't aware of the .replace method, it makes things much easier. Thanks for the response.
– KKH
Aug 12 at 3:39
Glad I could help. I feel you should go for the nested loop, this will simplify you code. rather than going for multiple single loops.
– argo
Aug 12 at 3:42
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Quick note: I have not included the first part of the question. it is : The company's domain has changed from (example.com) to (example.org)
– KKH
Aug 12 at 3:26