0

I have a dictionary that's formatted like this (it's missing the values, obv):

{'expand': ,
 'issues': [{'expand': ,
             'fields': {'created': ,
                        'issuetype': {'avatarId': ,
                                      'description': ,
                                      'iconUrl': ,
                                      'id': ,
                                      'name': ,
                                      'self': ,
                                      'subtask': },
                        'priority': {'iconUrl': ,
                                     'id': ,
                                     'name': ,
                                     'self': },
                        'project': {'avatarUrls': {'16x16': ,
                                                   '24x24': ,
                                                   '32x32': ,
                                                   '48x48': },
                                    'id': ,
                                    'key': ,
                                    'name': ,
                                    'projectCategory': {'description': ,
                                                        'id': ,
                                                        'name': ,
                                                        'self': },
                                    'projectTypeKey': ,
                                    'self': },
                        'status': {'description': ,
                                   'iconUrl': ,
                                   'id': ,
                                   'name': ,
                                   'self': ,
                                   'statusCategory': {'colorName': ,
                                                      'id': ,
                                                      'key': ,
                                                      'name': ,
                                                      'self': }}},
             'id': ,
---->        'key': ,
             'cccc': },

I need to get access to all the values that the key is "key".

jira_key = dict["issues"][0]["key"]

This grabs the key for the first one, but there's like 60+ that I need. I've tried various things I've found and nothing seems to be successful.

Note: Not a developer :)

  • Are you filtering just the `dict['issues']` list, or must you find any dictionary anywhere in the structure? – Martijn Pieters Jan 15 '19 at 16:33
  • If you are just trying to get `dict['issues'][0]['key']`, `dict['issues'][1]['key']`, etc (just the `key` values in the `dict['issues']` list), then search `for... loop` and you will be on your way. – benvc Jan 15 '19 at 16:36
  • So lets say you have dict: `some_dict = {'issues': [{'key':'bla'}, {'smth_else': 'bla'}]}` you can get filtered one like this: `new_dict = {'issues': [d for d in some_dict['issues'] if 'key' in d]}` Not sure if you if you want to get 'key' named 'key' so just for reference. If you want to get all the keys from python dict you can do `some_dict.keys()` – The Hog Jan 15 '19 at 16:46
  • I'm going to assume that you just want to extract the values for `key` from a single list. See the duplicate for that. – Martijn Pieters Jan 15 '19 at 17:01
  • Well I do want to get values from a list, but I've already got the first one. And from the looks of it, all the ticket data is nested in a dictionary within the list. So I'm not sure how to tell it to give me all values for the key "key", looping would've been a good idea. I don't know how to loop through a dictionary though? – bombasticbri Jan 15 '19 at 22:18

1 Answers1

-1

There are, as always, multiple ways which do the trick.

One of these ways, assuming for a second here the JIRA keys you need are only issue keys, not project or projectCategory keys, is by using the itemgetter:

from operator import itemgetter as itg
key_finder = itg('key')
my_values = list(map(key_finder, DICT_NAME['issues']))

The itemgetter functions as a wrapper around a comprehension and essentially returns tuple(obj[i] for i in items) where items is the collection of arguments passed into itemgetter. Therefore, itemgetter('key', 'id') would wrap a search for 'key' and 'id' and the function applied to a dictionary would return it's 'key' and 'id' values, in this case only 'key' was needed.

map(function, iterable) applies the function sequentially to items in iterable, e.g. function(iterable[0]); function(iterable[1]) etc.

In your case, with having to loop multiple times and combine results:

from functools import reduce
issue_key_values = reduce(lambda x,y: x+y, [list(map(key_finder, ll['issues'])) for ll in jira_list])

The reduction operator, from its docstring:

Apply a function of two arguments cumulatively to the items of a sequence, from left to right, so as to reduce the sequence to a single value.

The other steps are evaluation of generator expressions and loop comprehensions, which you can find plenty of material on around this site (and Google).

Uvar
  • 3,372
  • 12
  • 25
  • So this totally worked, but I have my code looping the data, since I can only pull x amount of tickets at a time. `loops = math.ceil(total/max)` is what I have to tell me how many times the loop needs to happen. I need to iterate on this, too. But I'm not sure how. `jira_keys = list(map(key_finder, jira_list[0]['issues'])) + list(map(key_finder, jira_list[1]['issues']))` is what is working right now. But I want it to go through the list per how many times my `loop` variable says. – bombasticbri Jan 16 '19 at 14:15
  • @bombasticbri try this one for size – Uvar Jan 16 '19 at 14:30
  • Ah! That's beautiful, thank you! But, if you're willing, can you explain some pieces of it, I just want to understand it better. :) – bombasticbri Jan 16 '19 at 14:45
  • If you like the solution, you can clear the negative vote on it / accept it as the working answer.. But I'll add some fluff – Uvar Jan 16 '19 at 14:57