0

I have this json and I want to extract only follower_ids w.r.t project_id into a list as :

followers_ids_for_01 = [123,124,...]
followers_ids_for_02 = [125,126,...]

Json List :

{
    "data": [{
        "id": 01,
        "followers": [{
            "id": 123,
            "name": "Shivkumar"
        }, {
            "id": 124,
            "name": "Sneha"
        },
        {
            .....
        }],
    }, {
        "id": 02,
        "followers": [{
            "id": 125,
            "name": "Teja"
        }, {
            "id": 126,
            "name": "Harsha"
        },
        {
            ......
        }],
    }]
}

What I tried :

data  =json_list
projects = data['data']

followers_ids =[]
for project in projects:
    for i in project['followers']:
       followers_ids.append( i['id'])

print followers_ids

Output:

[123, 124, 125, 126]

Can someone suggest How to generate list for "followers_ids_for_{project_id}" and add its respective followers ids in it?

Edit/Update:

I need to generate named variables for blank lists

datalists = dict()
for project in projects:
    datalists["followers_ids_for_{}".format(project['id'])] = []

print datalists
#{'followers_ids_for_1': [], 'followers_ids_for_2': []}
cs95
  • 379,657
  • 97
  • 704
  • 746
Shivkumar kondi
  • 6,458
  • 9
  • 31
  • 58
  • 1
    You will need a dictionary, you cannot "generate lists" otherwise. – cs95 Aug 10 '17 at 06:20
  • yes..I already went through some other answers like https://stackoverflow.com/a/42768290/6107715 still I m looking for a solution where I can get named blank lists – Shivkumar kondi Aug 10 '17 at 06:21
  • 1
    You mean, you want to have many separate variables names `followers_ids_for_01`, `followers_ids_for_02`, and so on? – cs95 Aug 10 '17 at 06:24
  • @cᴏʟᴅsᴘᴇᴇᴅ Yes.. If I go for dict , it will be like this : {'followers_ids_for_1': [], 'followers_ids_for_2': []} but I can use them as lists to add followers_Id – Shivkumar kondi Aug 10 '17 at 06:25
  • @BHappyBHarsham Why do you need `''followers_ids_for_...'` within your dict? Why not store the id in dict and create the string when you need to display the content. Your requirement doesn't makes any sense – Moinuddin Quadri Aug 10 '17 at 06:33
  • @MoinuddinQuadri I was trying to know the usage and limitations of list here. thanks for your suggestion – Shivkumar kondi Aug 10 '17 at 06:38
  • @BHappyBHarsham Also, variables are used for the code. I don't think you'll ever need the problem where you'll have to dynamically generate the variable name, though you can do it by [hacks](https://stackoverflow.com/a/45606063/2063361) but it is not something you'll encounter in real life – Moinuddin Quadri Aug 10 '17 at 06:41

3 Answers3

1

You could use a list comprehension instead

[follower['id'] for project in projects for follower in project['followers']]

if you were only interested in a particular project id

[follower['id'] for project in projects for follower in project['followers'] if project['id] == 'some id']
Josh Hamet
  • 957
  • 8
  • 10
1

You may use nest dict comprehension expressions to create the dict object`:

>>> {val['id']: [item['id'] for item in val['followers']] for val in data['data']}
{1: [123, 124], 2: [125, 126]}

Let's say the above dict is stored in variable result. Then you may access the element in dict corresponding to each so called project id as:

>>> result[1]    # For id '1'
[123, 124]

>>> result[2]    # For id '2'
[125, 126]

PS: data variable in the above dict comprehension holds the value of your JSON object, i.e. :

{
    "data": [{
        "id": 1,
        "followers": [{
            "id": 123,
            "name": "Shivkumar"
        }, {
            "id": 124,
            "name": "Sneha"
        }],
    }, {
        "id": 2,
        "followers": [{
            "id": 125,
            "name": "Teja"
        }, {
            "id": 126,
            "name": "Harsha"
        }]
    }]
}
Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126
1

The first thing you do is to build a dictionary in the format you want:

In [514]: f = {'followers_ids_for_%s' %d['id'] : [x['id'] for x in d['followers']] for d in data['data'] }; f
Out[514]: {'followers_ids_for_01': [123, 124], 'followers_ids_for_02': [125, 126]}

I recommend you stop here. However, here's the second, and highly non-recommended step (which comes from your hard requirement): Update globals().

In [516]: globals().update(f)

In [517]: followers_ids_for_01
Out[517]: [123, 124]

Now you can access those variables as lists. Once again, this is bad, as it pollutes your globals.

cs95
  • 379,657
  • 97
  • 704
  • 746
  • Actually I was trying to know the usage and limitations of list here for this scenario. You have given for such a better Explanation. thanks a lot – Shivkumar kondi Aug 10 '17 at 06:40
  • @BHappyBHarsham I'm glad you accepted the other answer, but I thought this was what you wanted. – cs95 Aug 10 '17 at 06:41