Skip to main content
Tweeted twitter.com/StackCodeReview/status/1499082111248224258
deleted 75 characters in body
Source Link
Andrew
  • 143
  • 1
  • 1
  • 7
def group_by_excluding_key(list_of_dicts, key_field):
    """
    Takes a list of `dict` items and groups by ALL KEYS in the dict EXCEPT the key_field.
    :param list_of_dicts: List of dicts to group
    :param key_field: key field in dict which should be excluded from the grouping
    """

    output = []

    for item in list_of_dicts:
 
        found = False
        search_groupitem_key = {k: v for k, v in item.itemspop() if k != key_field}
        item_value = item[key_field])

        for existing_group, found_keys in output:
            if existing_group.viewitems() == search_groupitem.viewitems():
                found_keys.append(item_valueitem_key)
                found = True
                break

        if not found:
            output.append((search_groupitem, [item_value][item_key]))

    return output
def group_by_excluding_key(list_of_dicts, key_field):
    """
    Takes a list of `dict` items and groups by ALL KEYS in the dict EXCEPT the key_field.
    :param list_of_dicts: List of dicts to group
    :param key_field: key field in dict which should be excluded from the grouping
    """

    output = []

    for item in list_of_dicts:
 
        found = False
        search_group = {k: v for k, v in item.items() if k != key_field}
        item_value = item[key_field]

        for existing_group, found_keys in output:
            if existing_group.viewitems() == search_group.viewitems():
                found_keys.append(item_value)
                found = True

        if not found:
            output.append((search_group, [item_value]))

    return output
def group_by_excluding_key(list_of_dicts, key_field):
    """
    Takes a list of `dict` items and groups by ALL KEYS in the dict EXCEPT the key_field.
    :param list_of_dicts: List of dicts to group
    :param key_field: key field in dict which should be excluded from the grouping
    """

    output = []

    for item in list_of_dicts:
        found = False
        item_key = item.pop(key_field)

        for existing_group, found_keys in output:
            if existing_group.viewitems() == item.viewitems():
                found_keys.append(item_key)
                found = True
                break

        if not found:
            output.append((item, [item_key]))

    return output
Rollback to Revision 8
Source Link
Andrew
  • 143
  • 1
  • 1
  • 7

I couldn't find a thing on Googlegoogle, so I wrote it pretty quickly, and I am wondering if there is a better way to do this (or a standard library even).

  • Naming things is hard... what is a better one?
  • This is a task I performed fairly often when receiving user input
  • The keys in each dict may not be consistent (some may be missing)
  • It is OK to destroy or mangle the original input list & items
  • Validation is done before calling, so 'key_field' is always present
  • It is expected that values can be duplicated in the list.
  • { status: 1 } => [1,2,3,4,1,2] is ok, if the values are actually present twice.

Simple little Pythonpython function:

I couldn't find a thing on Google, so I wrote it pretty quickly, and I am wondering if there is a better way to do this (or a standard library even).

  • Naming things is hard... what is a better one?
  • This is a task I performed fairly often when receiving user input
  • The keys in each dict may not be consistent (some may be missing)
  • It is OK to destroy or mangle the original input list & items
  • Validation is done before calling, so 'key_field' is always present
  • It is expected that values can be duplicated in the list
  • { status: 1 } => [1,2,3,4,1,2] is ok, if the values are actually present twice.

Simple little Python function:

I couldn't find a thing on google, so I wrote it pretty quickly, and I am wondering if there is a better way to do this (or a standard library even)

  • Naming things is hard... what is a better one?
  • This is a task I performed fairly often when receiving user input
  • The keys in each dict may not be consistent (some may be missing)
  • It is OK to destroy or mangle the original input list & items
  • Validation is done before calling, so 'key_field' is always present
  • It is expected that values can be duplicated in the list.
  • { status: 1 } => [1,2,3,4,1,2] is ok, if the values are actually present twice.

Simple little python function:

deleted 1 character in body
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

I couldn't find a thing on googleGoogle, so I wrote it pretty quickly, and I am wondering if there is a better way to do this (or a standard library even).

  • Naming things is hard... what is a better one?
  • This is a task I performed fairly often when receiving user input
  • The keys in each dict may not be consistent (some may be missing)
  • It is OK to destroy or mangle the original input list & items
  • Validation is done before calling, so 'key_field' is always present
  • It is expected that values can be duplicated in the list.
  • { status: 1 } => [1,2,3,4,1,2] is ok, if the values are actually present twice.

Simple little pythonPython function:

I couldn't find a thing on google, so I wrote it pretty quickly, and I am wondering if there is a better way to do this (or a standard library even)

  • Naming things is hard... what is a better one?
  • This is a task I performed fairly often when receiving user input
  • The keys in each dict may not be consistent (some may be missing)
  • It is OK to destroy or mangle the original input list & items
  • Validation is done before calling, so 'key_field' is always present
  • It is expected that values can be duplicated in the list.
  • { status: 1 } => [1,2,3,4,1,2] is ok, if the values are actually present twice.

Simple little python function:

I couldn't find a thing on Google, so I wrote it pretty quickly, and I am wondering if there is a better way to do this (or a standard library even).

  • Naming things is hard... what is a better one?
  • This is a task I performed fairly often when receiving user input
  • The keys in each dict may not be consistent (some may be missing)
  • It is OK to destroy or mangle the original input list & items
  • Validation is done before calling, so 'key_field' is always present
  • It is expected that values can be duplicated in the list
  • { status: 1 } => [1,2,3,4,1,2] is ok, if the values are actually present twice.

Simple little Python function:

Update example to show duplicated keys
Source Link
Andrew
  • 143
  • 1
  • 1
  • 7
Loading
python 2 tag because of viewkeys
Link
Graipher
  • 41.1k
  • 7
  • 69
  • 133
Loading
use pprint instead
Source Link
Andrew
  • 143
  • 1
  • 1
  • 7
Loading
deleted 11 characters in body
Source Link
Andrew
  • 143
  • 1
  • 1
  • 7
Loading
Rollback to Revision 2
Source Link
Andrew
  • 143
  • 1
  • 1
  • 7
Loading
deleted 5 characters in body
Source Link
Graipher
  • 41.1k
  • 7
  • 69
  • 133
Loading
added 71 characters in body
Source Link
Andrew
  • 143
  • 1
  • 1
  • 7
Loading
Source Link
Andrew
  • 143
  • 1
  • 1
  • 7
Loading