1

Question: # Write a program to modify the email addresses in the records dictionary to reflect this change

records = {57394: ['Suresh Datta', '[email protected]'], 48539: ['ColetteBrowning', '[email protected]'], 58302: ['Skye Homsi','[email protected]'], 48502: ['Hiroto Yamaguchi', '[email protected]'], 48291: ['Tobias Ledford', '[email protected]'], 48293: ['Jin Xu', '[email protected]'], 23945: ['Joana Dias', '[email protected]'], 85823: ['Alton Derosa', '[email protected]']}

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) 
4
  • 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) Commented Aug 12, 2018 at 3:26
  • Ok, then how about my answer Commented Aug 12, 2018 at 3:27
  • problem in last line, you cannot use ','.join(lst2) , lst2 is two-dimensional array Commented Aug 12, 2018 at 3:29
  • Appreciate your help. I understand the mistake I've made. Your answer worked as well. Commented Aug 12, 2018 at 3:37

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@KKH Glad I could help! If you've solved your problem, click the green check on my answer to accept it.
1

You can do a one-liner list-comprehension for, then iterate and do join the split of i with ',', so try this:

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:

['[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]']

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', '[email protected]'], 48539: ['ColetteBrowning', '[email protected]'], 58302: ['Skye Homsi', '[email protected]'], 48502: ['Hiroto Yamaguchi', '[email protected]'], 48291: ['Tobias Ledford', '[email protected]'], 48293: ['Jin Xu', '[email protected]'], 23945: ['Joana Dias', '[email protected]'], 85823: ['Alton Derosa', '[email protected]']}

Comments

0

Try the following code

records = {57394: ['Suresh Datta', '[email protected]'], 48539: ['ColetteBrowning', '[email protected]'], 58302: ['Skye Homsi','[email protected]'], 48502: ['Hiroto Yamaguchi', '[email protected]'], 48291: ['Tobias Ledford', '[email protected]'], 48293: ['Jin Xu', '[email protected]'], 23945: ['Joana Dias', '[email protected]'], 85823: ['Alton Derosa', '[email protected]']}
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', '[email protected]'], 48539: ['ColetteBrowning', '[email protected]'], 58302: ['Skye Homsi', '[email protected]'], 48502: ['Hiroto Yamaguchi', '[email protected]'], 48291: ['Tobias Ledford', '[email protected]'], 48293: ['Jin Xu', '[email protected]'], 23945: ['Joana Dias', '[email protected]'], 85823: ['Alton Derosa', '[email protected]']}

2 Comments

I wasn't aware of the .replace method, it makes things much easier. Thanks for the response.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.