+ 2

How to sort-out similar values from dictionaries and concatenet them as on the example below? (Python)

from operator import itemgetter def sort_by_age(student_list): student_list = [ {'name': 'Valentine', 'age': 30, 'city': 'Keln'}, {'name': 'Max', 'age': 20, 'city': 'Dallas'}, {'name': 'Bob', 'age': 32, 'city': 'LA'}, {'name': 'Andrew', 'age': 34, 'city': 'Keln'}, {'name': 'John', 'age': 50, 'city': 'Dallas'}, {'name': 'Colin', 'age': 21, 'city': 'LA'} ] v = {} for key, value in sorted(d.items()): v.setdefault(value, []).append(key) print("The list printed sorting by age: ") print(sorted(student_list, key=itemgetter('age', 'city'))) In the code above something missing and it is not working properly but what I need is output like this: Keln': [ {'name': 'Valentine', 'age': 30 }, {'name': 'Andrew', 'age': 34}], 'Dallas': [ {'name': 'Max', 'age': 20 }, {'name': 'John', 'age': 50}], 'LA': [ {'name': 'Bob', 'age': 32 }, {'name': 'Colin', 'age': 21} Thanks everybody ahead!

28th Feb 2020, 7:22 PM
Stanislav Voloshchuk
Stanislav Voloshchuk - avatar
1 Antwort
28th Feb 2020, 9:21 PM
Tibor Santa
Tibor Santa - avatar