I ask for help, it is impossible to loop through only the first element (dictionaries) of each tuple. The maximum that happened:

data = [ ({'a':a, 'b':b},[a,b]), ({'a':a, 'b':b},[a,b]), ({'a':a, 'b':b},[a,b]) ] a = lambda a: aa for tuples in data: for dict_and_list in tuples: print('\n', dict_and_list) 

I get the output: {'a': a, 'b': b} [a, b], etc. It is necessary to go through the dictionary and group it by a certain value. The problem is that I cannot figure out how to make the next cycle run only on the first element of the tuples (dictionaries) in order to further group it.

Got to this option, but now it is impossible to group and display together the elements of the dictionary and the elements of the list.

  a = lambda a: aa tags = [tuples[0] for tuples in data] name_and_path = [tuples[1] for tuples in data] group_by = groupby(tags, a) for key, info in group_by: print("\n", key) for tag in info: print('{0}'.format(tag.a)) 

    1 answer 1

    Dictionaries in Python are неупорядоченные collections of arbitrary objects with access by key.

     data = [ ({'a': 'a1', 'b': 'b1'}, ['a11','b11']), ({'a': 'a2', 'b': 'b2'}, ['a12','b12']), ({'a': 'a3', 'b': 'b3'}, ['a13','b13']), ({'b': 'b4', 'a': 'a4'}, ['a14','b14']) ] result = [(tuples[0]['a'],tuples[0]['b'],tuples[1]) for tuples in data] from pprint import pprint as pp pp(result) [('a1', 'b1', ['a11', 'b11']), ('a2', 'b2', ['a12', 'b12']), ('a3', 'b3', ['a13', 'b13']), ('a4', 'b4', ['a14', 'b14'])] 
    • You misunderstood (apparently I did not properly describe the question), the dictionary is the first element of each tuple, the second list. It is necessary to go through the first element of the tuple and group them by a specific key value (the second part of the code in question), but also display in each group other dictionary and list values ​​(the second element of the tuple). Without a list (the second element of the tuple) everything works out, but I cannot think of how to insert another list into grouping and output - MrStepin
    • Please give an example of the input data and the result you want to get. - S. Nick
    • An example of input data is an array of data - a list of tuples, in each tuple the first element is a dictionary, and the second list. It is necessary to group the output through groupby by one of the dictionary values ​​and then in each group to display all the remaining values ​​of the dictionary and the list that belong to this group. If you discard the list, then everything works out (sample code at the end of the question), but you need to do this along with the list. Example output: a1 is the first group, under it are the other dictionary values ​​in which there is a1 value, as well as all list items from tuples that have a dictionary with a1 value - MrStepin
    • try this: [(tuples[0]['a'],tuples[0]['b'],tuples[1]) for tuples in data] - S. Nick
    • Thanks, in the morning I will try and write off what happened - MrStepin