0

I'm trying to iterate a dictionary that is into another dictionary to get 2 values (content and completed_date) of each set but I don't know how to do it. This is the data:

{  
   'items':[  
      {  
         'content':'Get a solution for the server @connect',
         'meta_data':None,
         'user_id':20440353,
         'task_id':3186948449,
         'project_id':2203217746,
         'completed_date':'Tue 07 May 2019 18:31:47 +0000',
         'id':3186948449
      },
      {  
         'content':'Fix the Placement test based on the feedback @connect',
         'meta_data':None,
         'user_id':20440353,
         'task_id':3186363895,
         'project_id':2203217746,
         'completed_date':'Tue 07 May 2019 14:52:27 +0000',
         'id':3186363895
      }
   ],
   'projects':{  

   }
}

This is what I've been trying:

>>> for items in itstuff_data['items']:
...     print(items)
... 

But is giving me everything inside of 'items' (as expected). Is there a way to iterate the data to get just content and completed_date?

martineau
  • 119,623
  • 25
  • 170
  • 301
  • Possible duplicate of [Iterating over dictionaries using 'for' loops](https://stackoverflow.com/questions/3294889/iterating-over-dictionaries-using-for-loops) – sahasrara62 May 12 '19 at 22:38
  • `print( items['content'], items['completed_date'] )` – furas May 12 '19 at 22:50

4 Answers4

2

In your loop, items will probably always be a dictionary; to now access the "content" and the "completed_date", you can just index into that dictionary:

items["content"]
items["completed_date"]
henriman
  • 155
  • 1
  • 11
1

here is one way, you just need to loop again!

itstuff = {  
   'items':[  
      {  
         'content':'Get a solution for the server @connect',
         'meta_data':None,
         'user_id':20440353,
         'task_id':3186948449,
         'project_id':2203217746,
         'completed_date':'Tue 07 May 2019 18:31:47 +0000',
         'id':3186948449
      },
      {  
         'content':'Fix the Placement test based on the feedback @connect',
         'meta_data':None,
         'user_id':20440353,
         'task_id':3186363895,
         'project_id':2203217746,
         'completed_date':'Tue 07 May 2019 14:52:27 +0000',
         'id':3186363895
      }
   ],
   'projects':{  

   }
}



for obj in itstuff["items"]:
    for key in obj:
        if (key == "content" or key == "completed_date"):
            # Do something with values
            print(obj[key])
Jack Hamby
  • 198
  • 1
  • 10
1

This is a good use-case for operator.itemgetter:

from operator import itemgetter

# foo(d) == (d['content'], d['completed_date'])
foo = itemgetter("content", "completed_date")

for content, completed_date in map(foo, itstuff_data['items']):
    ...
chepner
  • 497,756
  • 71
  • 530
  • 681
0
data=[]
for dic in itstuff_data['items']:
         data.append((dic['content'],dic['completed_date']))
         print(dic['content'],dic['completed_date'])

you can also iterate through a dictionary object as

for k,v in dic.items() or for item in dic: print(dic[item])

sahasrara62
  • 10,069
  • 3
  • 29
  • 44
  • itstuff_data["items"] should be a list with dictionary, therefore items will always be a dictionary already, in which you can index. Your second loop is not needed or rather wrong in this context. – henriman May 12 '19 at 22:38