Joliet
Joliet

Reputation: 101

Group repeating JSON key value pairs together in list with Python

I have read and filtered the data I needed from REST API like so:

 [[key,value] for groups in results for group in groups['results'] for key,value in group.items() if key in ['utc_offset','country','city','state','name','link','lat','lon']]

My output:

[['utc_offset', -14400000],
['country', 'CA'],
['city', 'Toronto'],
['link', 'https://www.meetup.com/slashdot-3/'],
['lon', -79.44000244140625],
['name', 'The Toronto Slashdot Meetup Group'],
['state', 'ON'],
['lat', 43.7599983215332],
['utc_offset', -14400000],
['country', 'CA'],
['city', 'Toronto'],
['link', 'https://www.meetup.com/webcentric/'],
['lon', -79.44000244140625],
['name', 'The Greater Toronto Web Centric Meetup Group'],
['state', 'ON'],
['lat', 43.7599983215332],
['utc_offset', -14400000],
['country', 'CA'],
['city', 'Toronto'],
['link', 'https://www.meetup.com/php-27/'],
['lon', -79.37999725341797],
['name', 'The Toronto PHP Meetup Group'],
['state', 'ON'],
.
.
.
]]

What I'm attempting to do is group the repeating keys like so they can be modeled into data objects for Django:

[['utc_offset', -14400000],['country', 'CA'],['city', 'Toronto'],['link', 'https://www.meetup.com/slashdot-3/'],['lon', -79.44000244140625],['name', 'The Toronto Slashdot Meetup Group'],['state', 'ON'],['lat', 43.7599983215332]]

I've tried this:

[zip(range(8),[key,value]) for groups in results for group in groups['results'] for key,value in group.items() if key in ['utc_offset','country','city','state','name','link','lat','lon']]

But my output is this:

<zip object at 0x00000228D3C2F388>
<zip object at 0x00000228D3C2F388>
<zip object at 0x00000228D3C2F388>
<zip object at 0x00000228D3C2F388>
<zip object at 0x00000228D3C2F388>
<zip object at 0x00000228D3C2F3C8>
<zip object at 0x00000228D3C2F3C8>
<zip object at 0x00000228D3C2F3C8>
<zip object at 0x00000228D3C2F3C8>
<zip object at 0x00000228D3C2F388>
 .
 .
 .
 # twice the amount of the records I already have

So this is pretty much not the answer, so any help would be great. Thanks!

EDIT My desired output would be like so

[['utc_offset', value],['country',value],['city',value],['link',value],['lon',value],['name',value],['state',value],['lat',value]]

Upvotes: 2

Views: 479

Answers (1)

Darkstarone
Darkstarone

Reputation: 4730

I think this is what you're after (I'm still not 100% sure what you want me to do with each block of results):

l = [['utc_offset', -14400000],
['country', 'CA'],
['city', 'Toronto'],
['link', 'https://www.meetup.com/slashdot-3/'],
['lon', -79.44000244140625],
['name', 'The Toronto Slashdot Meetup Group'],
['state', 'ON'],
['lat', 43.7599983215332],
['utc_offset', -14400000],
['country', 'CA'],
['city', 'Toronto'],
['link', 'https://www.meetup.com/webcentric/'],
['lon', -79.44000244140625],
['name', 'The Greater Toronto Web Centric Meetup Group'],
['state', 'ON'],
['lat', 43.7599983215332],
['utc_offset', -14400000],
['country', 'CA'],
['city', 'Toronto'],
['link', 'https://www.meetup.com/php-27/'],
['lon', -79.37999725341797],
['name', 'The Toronto PHP Meetup Group'],
['state', 'ON']]

result = []

tmp = []

for item in l:
    if item[0] == 'lat':
        tmp.append(item)
        result.append(tmp)
        tmp = []
    else:
        tmp.append(item)

print(result)

Which results in:

[
    [
        ['utc_offset', -14400000], 
        ['country', 'CA'], 
        ['city', 'Toronto'], 
        ['link', 'https://www.meetup.com/slashdot-3/'], 
        ['lon', -79.44000244140625], 
        ['name', 'The Toronto Slashdot Meetup Group'], 
        ['state', 'ON'], 
        ['lat', 43.7599983215332]
    ], 
    [
        ['utc_offset', -14400000], 
        ['country', 'CA'], 
        ['city', 'Toronto'], 
        ['link', 'https://www.meetup.com/webcentric/'], 
        ['lon', -79.44000244140625], 
        ['name', 'The Greater Toronto Web Centric Meetup Group'], 
        ['state', 'ON'], 
        ['lat', 43.7599983215332]
    ]
]

Each block of utc_offset to lat gets put in its own list. If you need it in some other format, let me know.

Upvotes: 1

Related Questions