0

Suppose I have a series of tweets in the form of a dictionaries in the following structure:

Tweet = {
'created_at': 'Thu Apr 21 16:03:40 +0000 2016', 
'text': 'Look at this dog!', 
'entities': {'hashtags': ['dog'] }, 
'favorite_count': 0, 
'user': {'screen_name': 'Jake_Jake'}, 
'retweet_count': 0 
}

I'd like to create a new dictionary so that the 'hashtags' and 'screen_name' key and value is no longer nested:

{'created_at': 'Thu Apr 21 16:03:40 +0000 2016', 'text': 'Look at this dog!', 'favorite_count': 0, 'hashtags': ['dog'], 'retweet_count': 0, 'screen_name': 'Jake_Jake'}

Any suggestion as to how I could accomplish this? Thank you.

lord63. j
  • 4,500
  • 2
  • 22
  • 30

1 Answers1

2

Try this,

d = dict()
for k, v in iTweet.items():
    if isinstance(v, dict):
        for k2, v2 in v.items():
            d[k2] = v2
    else:
        d[k] = v

print(d)
{'screen_name': 'Jake_Jake', 'text': 'Look at this dog!', 'created_at': 'Thu Apr 21 16:03:40 +0000 2016', 'hashtags': ['dog'], 'retweet_count': 0, 'favorite_count': 0}

More general, extract all the key, value pairs from a nested dict by using a recursive function,

def extract(dict_in, dict_out):
    """extract all the key, value pairs from a nested dict"""
    for key, value in dict_in.iteritems():
        if isinstance(value, dict): # value itself is dictionary
            extract(value, dict_out)
        else:
            dict_out[key] = value
    return dict_out

# Test
dict_out = dict()
extract(iTweet, dict_out)

print(dict_out)
# Output
{'screen_name': 'Jake_Jake', 'text': 'Look at this dog!', 'created_at': 'Thu Apr 21 16:03:40 +0000 2016', 'hashtags': ['dog'], 'retweet_count': 0, 'favorite_count': 0}

Refer to how to get all keys&values in nested dict of list-of-dicts and dicts?.

Community
  • 1
  • 1
SparkAndShine
  • 17,001
  • 22
  • 90
  • 134