Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

python: loop through specific key value

I’m trying to process following dictionary.

thisdict =  {
    "changes": [
        "abc",
        "cba"
    ],
    "projects": [
         "aaa"
    ],
}

added_change=thisdict["changes"]

for x, y in added_change.items():
  print(added_change)

desired output

abc
cba

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You will take the error like this:

AttributeError: 'list' object has no attribute 'items'

Because added_change is a list, not a dictionary and items is used with dictionary.

Your added_change list includes already ['abc', 'cba'], you want to print them line by line, thus, you only need to use print with iteration on your list.

for i in added_change:
    print(i)

Output:

abc
cba
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading