bluethundr
bluethundr

Reputation: 1325

Iterate over dictionaries' entries inside a list in python

I have a dictionary that sits inside of a list in python. How do I iterate over it? I need to get at both the keys and the values:

 z = (client.list_signing_certificates(UserName=user_name)['Certificates'])
print(z)
[{'UserName': 'tdunphy', 'CertificateId': 'MSLQC5IFBTSNJOBEMIYXNN3DWC372237', 'CertificateBody': '-----BEGIN CERTIFICATE-----\nMIICWzCCAcSgAwIBAgIJAOrfAe4tcD4yMA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNV\nBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX\naWRnaXRzIFB0eSBMdGQwHhcNMTkwNDI0MTMxMjUzWhcNMjkwNDIxMTMxMjUzWjBF\nMQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50\nZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB\ngQDUMtMJ89aEflMt8HIB/wXxDVmCCy+M6asQC66oCIlXZA9Jrxc6ixLcI0Xhj0Fc\n/dU9VxAiVI9/ERlzO0FtSslWW9Qj3+Pp/XUyzeNDbI0VNIl8v4iQNbt5DLUbMBFI\nbbLheDJNt+xlSZK6cq5r7do0ChI9cZVQeAH9bvWkpPbiSQIDAQABo1MwUTAdBgNV\nHQ4EFgQUuLbgt2ftH2USaJ8Xku1ajdKL6r4wHwYDVR0jBBgwFoAUuLbgt2ftH2US\naJ8Xku1ajdKL6r4wDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG9w0BAQUFAAOBgQCN\nTCPXqPXyEQsWlUDT0A0Pjt3E5pELlUZ4jfUf/wU1OJDOZhv8jCr1nIcnYEECnjdS\noGifOBPeyJxKfUoSoFGNFHfgkaIRn0LaTGHCiyQQZNAnX9Jfqf7AAK8iSXphgthH\nUWQqyrpL83zcGAfIa3hyez3iEZ/P9zMEPSuhuwIbIA==\n-----END CERTIFICATE-----\n', 'Status': 'Active', 'UploadDate': datetime.datetime(2019, 4, 24, 13, 32, 31, tzinfo=tzutc())}, {'UserName': 'tdunphy', 'CertificateId': 'JPVTB5JCCAZ4COLGLG3QT2E2HBRKVZZP', 'CertificateBody': '-----BEGIN CERTIFICATE-----\nMIICWzCCAcSgAwIBAgIJAJmLouu5GeqxMA0GCSqGSIb3DQEBBQUAMEUxCzAJBgNV\nBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBX\naWRnaXRzIFB0eSBMdGQwHhcNMTkwNDI0MTMzNDIyWhcNMjkwNDIxMTMzNDIyWjBF\nMQswCQYDVQQGEwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50\nZXJuZXQgV2lkZ2l0cyBQdHkgTHRkMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB\ngQDUMtMJ89aEflMt8HIB/wXxDVmCCy+M6asQC66oCIlXZA9Jrxc6ixLcI0Xhj0Fc\n/dU9VxAiVI9/ERlzO0FtSslWW9Qj3+Pp/XUyzeNDbI0VNIl8v4iQNbt5DLUbMBFI\nbbLheDJNt+xlSZK6cq5r7do0ChI9cZVQeAH9bvWkpPbiSQIDAQABo1MwUTAdBgNV\nHQ4EFgQUuLbgt2ftH2USaJ8Xku1ajdKL6r4wHwYDVR0jBBgwFoAUuLbgt2ftH2US\naJ8Xku1ajdKL6r4wDwYDVR0TAQH/BAUwAwEB/zANBgkqhkiG9w0BAQUFAAOBgQAK\nI2kAzJDnqfAMcsqhTBJRF5zdE4oIw9EhjJLb0S462EVzZARIeTkqhn8nCAiigtJy\nkxZHGRCiVEFL0z1932vyB2e4crvIsLD7CYWQgxI7cLaGAyRcVIWs5i+qbrw7zOkU\nxmMtlfD3C3ju4bH9+bO2rymBynJldfARujrv0oVTcw==\n-----END CERTIFICATE-----\n', 'Status': 'Active', 'UploadDate': datetime.datetime(2019, 4, 24, 13, 35, 2, tzinfo=tzutc())}]

There are 2 certificates in the list. I need to get at both. If there are more certificates I need to get at those too. It varies based on the user.

I am using python 3.

Upvotes: 0

Views: 89

Answers (4)

Geekfish
Geekfish

Reputation: 2314

You can iterate over the list, then iterate over the dictionary items:

for element in z:
    for key, value in element.items():
        print("Key is: ", key)
        print("Value is: ", value)

dict.items() returns a tuple that contains each key and the associated value.

Otherwise, if you only need keys you can use dict.keys(), or, if you only need values, you can use dict.values().

Edit:

If what you are looking is to get the certificate body then you don't even need to iterate the dictionaries, you could do:

certificates = []

for element in z:
   certificates.append(element["CertificateBody"])

or as a comprehension:

certificates = [element["CertificateBody"] for element in z]

Upvotes: 1

pe.kne
pe.kne

Reputation: 684

You need to iterate through the list first. Then for every dictionary in the list, iterate through the dictionary.

for i, d in z:
    for y, value in d:
        print(key)
        print(value)

Upvotes: -1

Houssem Gharbi
Houssem Gharbi

Reputation: 33

Don t know whats the problem exactly:

a={'a':1,'b':3}
b= {'c':1,'d':3}
l=[a,b]
for i in l :
    print(i)
    print(i.keys())
    print(i.values())

Upvotes: 1

Sergey Ivanov
Sergey Ivanov

Reputation: 3929

You just get a dictionary and then iterate over items of dictionary.

for key, value in z[0].items():
    print(key, value)

Upvotes: 0

Related Questions