-1

I found the following dictionary:

catalog = {"threads":{"39894014":{"date":1390842451,"r":0,"i":0,"lr":  {"id":39894014},"semantic_url":"the-g-wiki-g-is-for-the-discussion-  of-technology","sticky":1,"closed":1,"capcode":"mod"}}

I know I could access "date" for example by writing data["threads"]["39894014"]["date"], but is there some way of skipping the second thing (["39894014"],) because this number is randomly generated so the list may change, and it would be a totally different number.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
IThelp
  • 57
  • 1
  • 1
  • 8

1 Answers1

0

You'll have to loop over the keys or values of the dictionary value of the 'threads' key; if there is only ever one thread in the dictionary, you can extract that one dictionary with:

thread = next(data['threads'].values())
print(thread['date'])

If you don't need the outer dictionary to remain pristine, you could just use dict.popitem() to unwrap:

threadid, thread = data['threads'].popitem()
print(thread['date'])

To get all entries you can loop and grab the dates:

all_dates = [thread['data'] for thread in data['threads'].values()]
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343