0

I'm a beginner at Python and maybe I am trying to do something a little complex - I don't know. I'm scratching my brain on how to do this and what the right way would be to go about it, i.e. proper steps?

Below is what I have is a Dictionary as such:

[{'\id':'001', 
  'second_id':'0001', 
  'imp_url':"https://urlone.com", 
  'last_id':'00001'},

  {'\id':'002', 
  'second_id':'0002', 
  'imp_url':"https://urltwo.com", 
  'last_id':'00002'}
 ]

And here is my List:

['imp_url', '["https://urltestx.com", "https://urltestxx.com"]']

Desired result is an updated object that would have updated values* for the key "imp_url", which exist in the List(above). Like so:

Desired result:

[{'\id':'001', 
  'second_id':'0001', 
  'imp_url':'["https://urltestx.com"]', 
  'last_id':'00001'},

  {'\id':'002', 
  'second_id':'0002', 
  'imp_url':'["https://urltestxx.com"]', 
  'last_id':'00002'}
 ]

Note* - Brackets and single quotes are kept in updated desired result.

I hope that made sense. I tried using dict with zip, then went down a rabbit hole of frozensets? I'm pretty sure I need to create a new Dict but how can I can update an object from values in an array to specific key? I also tried using .update() but that seemed to work with integers as key and not necessarily words as keys. I also don't know about Tuples yet.

Is there a method to do this?

Any assistance would be really helpful! Little lost ... Thank you all -

3
  • I am on python version 3.9 Commented May 20, 2021 at 1:05
  • Where did the list come from? The second element looks like JSON. Commented May 20, 2021 at 1:43
  • the List I made, it served a purpose to be modified now i need to take contents and insert it into a specific key Commented May 20, 2021 at 1:55

1 Answer 1

1

You were in the right direction, you can use ast.literal_eval to make the second element of your list into an actual list.

seq = [{'\id':'001', 
  'second_id':'0001', 
  'imp_url':"https://urlone.com", 
  'last_id':'00001'},

  {'\id':'002', 
  'second_id':'0002', 
  'imp_url':"https://urltwo.com", 
  'last_id':'00002'}
 ]

from ast import literal_eval

new_list = ['imp_url', '["https://urltestx.com", "https://urltestxx.com"]']
new_list[-1] = literal_eval(new_list[-1])


for d, url in zip(seq, new_list[-1]):
    d[new_list[0]] = f'["{url}"]'

print(seq)

Output

[{'\\id': '001',
  'second_id': '0001',
  'imp_url': '["https://urltestx.com"]',
  'last_id': '00001'},
 {'\\id': '002',
  'second_id': '0002',
  'imp_url': '["https://urltestxx.com"]',
  'last_id': '00002'}]
Sign up to request clarification or add additional context in comments.

20 Comments

can you edit your question to explain this more?
is there a reason you want that?
"['https://urltestxx.com']" getting this is not much change, double quotes outside, single quotes inside, getting the other way you want needs some changes, is the first form ok?
If it is not too much trouble, single quotes outside and double quotes inside would be fantastic ^_^
check the new edit @knowledgealways, can you upvote and accept if it helped you?
|

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.