0

I'm using python 3.8 and I have list

 { "A1":[{"id": 1,"name": "abc", "isdeleted":true},{"id":2,"name": "pqr", "isdeleted":false},{"id": 3,"name" : "xyz", "isdeleted":false}]}

I want to iterate in dictionary and get result like

X = [1,2,3]

3 Answers 3

4

Why not [i["id"] for i in X]?

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

Comments

1

*note also change your boolean values to big letter "T" or "F", True not true and False not false.

myDict = {"A1": [
                {"id": 1, "name": "abc", "isdeleted": True}, 
                {"id": 2, "name": "pqr", "isdeleted": False}, 
                {"id": 3, "name": "xyz", "isdeleted": False}
                ]
                }
x = []

for item in myDict["A1"]:
    x.append(item["id"])

print(x)

output [1, 2, 3]

or you can use the shorter way:

myDict = {"A1": [
                {"id": 1, "name": "abc", "isdeleted": True}, 
                {"id": 2, "name": "pqr", "isdeleted": False}, 
                {"id": 3, "name": "xyz", "isdeleted": False}
                ]
                }
x = [item['id'] for item in myDict['A1']]

print(x)

3 Comments

I have updated the question can you please help me out on that
I've updated my answer. If this helped you, it would be great if you mark my answer as useful. :)
Nice. If you would mark my answer as useful, that would be great.
0

We can a create a list of type dict like these, change the dict as below. After slice the list.

myDict = {"A1":[{"id": 1,"name": "abc", "isdeleted":true},{"id": 2,"name": "pqr", "isdeleted":false},{"id": 3,"name": "xyz", "isdeleted":false}]}

x=[]
for i in X["A1"]:
    x.append(X['id']) 
Print(x)

2 Comments

I have updated the question, can you please help me out with that
I have edited my answer according to your question. It may help you.

Your Answer

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