Questions tagged [itertools-groupby]

Function in the itertools module. Makes an iterator that returns **consecutive keys** and groups from the iterable (note the difference from similarly named GROUPBY in SQL, pandas etc. which groups rows by the same values). Be sure to include the `python` tag for increased visibility.

groupby method in the itertools module makes an iterator that returns groups from an iterable.

Unlike the similarly named GROUPBY in SQL databases or data manipulation packages such as (which group by common elements regardless of order), itertools.groupby() creates a new group every time the value of the key changes. For example, [1,1,2,1] is three groups: [[1,1], [2], [1]]. To mimic the GROUPBY in SQL, it is often needed to sort the input by the keys to be grouped on.

Reference / related tags:

25 questions
0
votes
1 answer

Python: Value disappears from the list when using groupby and converting to a dictionary

I am trying to write a simple function that gives the result of a runoff vote. I start with a nested list with names of candidates, and I would like to group them by the first element and put that into a dictionary (where the first element is the…
0
votes
2 answers

How to filter a collection of objects by field value?

How in Python to organize and filter a collection of objects by a field value? I need to filter by being equal to an exact value and by being less than a value. And how to do it effectively? If I store my objects in a list I need to iterate over a…
0
votes
2 answers

Groupby a list of dicts with multiple keys doesn't work even if I use sorted before

I have a list of dictionaries like that : a = [ {'user_id':'111','clean_label':'VIR SEPA'}, {'user_id':'112','clean_label':'VIR SEPA'}, {'user_id':'111','clean_label':'VIR SEPA'}, ] and I want that : a = [ [ …
Quxntin
  • 39
  • 5
0
votes
1 answer

Python increment rows starting from a month on month

How to obtain the below result Current Month is the column which is to be calculated. We need to get the increment every month starting from Jan-18 for every account id. Every Account First row/ Record will start from JAN-18, and Second Row will be…
0
votes
1 answer

grouping multiple items in list into multiple groups of different size python

I am trying to group .xls files in list infiles based on strings in the .xls file names. The file names are formatted like this "type_d_cross_profile_glacier_name_A-Z" where type_d is a type of glacier environment, the glacier_name is each glacier,…
-1
votes
2 answers

How to group or batch the list of elements based on indices sublist?

How to group or batch the list of elements based on indices sublist ? Below are the possible instances should be considered. Hope that helps better. 1st Instance : Length of elements == sum of all indices elements indices = [1, 3, 5] elements =…
-1
votes
1 answer

How to print list of objects grouped by itertools' groupby?

I have a class with some fields. I created a list of objects of this class and now I want to group the list by one of the fields inside the objects. It almost works, but unfortunately it shows me only one Dog, but the result for this data should be…
mazix
  • 2,540
  • 8
  • 39
  • 56
-1
votes
2 answers

Python - itertools.groupby 2

Just having trouble with itertools.groupby. Given a list of dictionaries, my_list= [ "AD01", "AD01AA", "AD01AB", "AD01AC", "AD01AD","AD02", "AD02AA", "AD02AB", "AD02AC"] from this list, I expected to create a dictionary, where the key is the…
Bgamboa
  • 3
  • 1
-2
votes
2 answers

List of strings I want to group together if they contain a specific substring from a master list

I have list of strings that I want to group together if they contain a specific substring from a master list. Example Input: ["Audi_G71Q3E5T7_Coolant", "Volt_Battery_G9A2B4C6D8E", "Speaker_BMW_G71Q3E5T7", "Engine_Benz_G9A2B4C6D8E",…
Gilles
  • 1
-3
votes
2 answers

Python - Group by any matching value

I have a data structure like this [ (123, 321), (123, 456), (999, 654), (111, 456), (999, 898), (111, 654), (481, 739), ] How can I group the tuples together by any matching element? i.e. to get this result (order doesn't matter) [ …
Mark
  • 109
  • 1
  • 11
1
2