-2

I have this input object:

vv = [{'values': ['AirportEnclosed', 'Bus', 'MotorwayServiceStation']},{'values': ['All']}]

...there can be variable numbers of dicts present, but all dicts will always have the key 'values' and values populated for this.

The type of value assigned to 'values' will always be string or list. I wish to group/zip so I get the following output (list of tuples or tuple of tuples is fine):

(
('AirportEnclosed', 'All'),
('Bus', 'All'),
('MotorwayServiceStation', 'All')
)

...this is my code:

import itertools

outputList=[]
for i,g in itertools.groupby(vv, key=operator.itemgetter("values")):
    outputList.append(list(g))
print(outputList) 

...and this is my output:

[[{'values': ['AirportEnclosed', 'Bus', 'MotorwayServiceStation']}], [{'values': ['All']}]]

...what do I need to change?

gdogg371
  • 3,879
  • 14
  • 63
  • 107
  • This is effectively a duplicate of your [earlier question](https://stackoverflow.com/questions/75537113/how-to-zip-keys-within-a-list-of-dicts), where the requirement was also ambiguous. If multiple people are telling you the same thing, maybe they're right and you're not. – jarmod Feb 23 '23 at 12:51
  • i asked about zipping last time and the answer provided did not reference itertools. groupby...i was asked to provide additional information, which i have done. if you dont know the answer though thats fine. thanks for your input. – gdogg371 Feb 23 '23 at 12:54
  • 1
    Regardless of how you want the solution constructed, the lack of an unambiguous requirement means that we can only guess at what you want. I know that sounds harsh, but 4 people have tried to help you so far. We want to help you, but we can't (beyond guessing). – jarmod Feb 23 '23 at 12:56
  • 2
    OK, let me provide 2 examples (of many) that would be valid inputs according to your question, but for which we can not infer the desired result: 1) a list with 17 dicts, all of which have a `values` key and 2) your current example but where the 2nd dict's `values` is not `['All']` but is something else that is valid, e.g. `['cat', 'dog', 'mouse']`. Now, you may say "oh well those are obviously invalid inputs" but they are perfectly valid according to your requirement, and it should therefore be obvious to you that the requirement is indeed ambiguous. – jarmod Feb 23 '23 at 15:29
  • why in your opinion is ['cat', 'dog', 'mouse'] valid, but ['All'] isnt? – gdogg371 Feb 23 '23 at 15:39
  • 2
    You misunderstand. I am not saying that `['All']` is invalid. I am saying that "The type of value assigned to 'values' will always be string or list" means that `values` could be, for example `'a'` or `['a']` or `['a', 'b', 'c']` (or other things). What should the result be if the 2nd dict's `values` is `['a', 'b', 'c']`? – jarmod Feb 23 '23 at 16:14

1 Answers1

2
import itertools as it

vv = [{'values': ['AirportEnclosed', 'Bus', 'MotorwayServiceStation']}, 
{'values': ['All']}]

tmp = []

for curr_dict in vv:
    val = curr_dict["values"]
    if type(val) == str:
        val = [val]
    tmp.append(val)

pr = it.product(*tmp)
out = []

for i in pr:
    out.append(i)
trigonom
  • 528
  • 4
  • 9