Home Ask Login Register

Developers Planet

Your answer is one click away!

Bijan February 2016

Python Group Array by Column and Display Unique Values

I have an Array of Arrays with following format:

x = [["Username1","id3"],
["Username1", "id4"],
["Username1", "id4"],
["Username3", "id3"]]

I want to group by the ids and display all the unique usernames How would I get an output that is like:

id3: Username1, Username3

id4: Username1

Edit: Was able to group by second column but I cannot only display unique values. Here is my code:

for key, group in groupby(sorted(x), key=lambda x: x[1]):
    data[key]=[v[0] for v in group]


Andriy Ivaneyko February 2016

Use dict to create unique keys by id and pythons sets to store values ( so you would store only unique names for that keys):

items = [
    ["Username1", "id4"],
    ["Username1", "id4"],
    ["Username3", "id3"]

data = {}
for item in items:
    if data.has_key(item[1]):
        data[item[1]] = set([item[0]])

Aliminator February 2016

You may use a for loop but using a linq statement might be cleaner for future usage.


has some great ways to incorpurate linq to solve this issue. I think what you are looking for would be grouping by.


from collections import defaultdict
from operator import attrgetter

   def group_by(iterable, group_func):
       groups = defaultdict(list)
       for item in iterable:
       return groups

   group_by((x.foo for x in ...), attrgetter('bar'))

Post Status

Asked in February 2016
Viewed 1,302 times
Voted 8
Answered 2 times


Leave an answer

Quote of the day: live life