0

from the given input

lists = ["7ee57f24", "deadbeef"]

I want to get the following output

l1': [
   {
   'd': 
         {
            'id': '7ee57f24'
         }
   },
   {
   'd': 
         {
            'id': 'deadbeed'
         }
   }
]

I have tried this code

    lists = ["7ee57f24", "deadbeef"]
    l1 = {"d":[{"id": lis} for lis in lists]}
    print(l1)

but it gives me wrong output

{'d': [{'id': '7ee57f24'}, {'id': 'deadbeef'}]}
1
  • is l1 a list or a dict? Commented Jul 13, 2021 at 16:40

1 Answer 1

3

Use the following:

lists = ["7ee57f24", "deadbeef"]
l1 = [
    {"d": {"id": id_}}
    for id_ in lists
]
print(l1)

Output:

[{'d': {'id': '7ee57f24'}}, {'d': {'id': 'deadbeef'}}]
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. Can you please explain what I was missing
@kharevbv, You were trying to put dicts in a dictionary when you really wanted to put dicts in a list.

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.