Advertisement
Guest User

Untitled

a guest
Mar 9th, 2021
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.19 KB | None | 0 0
  1. def beautify_response(response):
  2.     new_response = {'users': []}
  3.  
  4.     specialties = {}
  5.     for specialty in response['specialties']:
  6.         specialties[specialty['id']] = {'id': specialty['id'], 'title':specialty['title']}
  7.  
  8.     for user in response['users']:
  9.         new_response['users'].append({})
  10.         new_response['users'][-1]['id'] = user.get('id')
  11.         new_response['users'][-1]['surname'] = user.get('surname')
  12.         new_response['users'][-1]['avatar_id'] = user.get('avatar_id')
  13.         new_response['users'][-1]['specialty_ids'] = []
  14.         for specialty in user.get('specialty_ids'):
  15.             new_response['users'][-1]['specialty_ids'].append({})
  16.             new_response['users'][-1]['specialty_ids'][-1]['id'] = specialties[specialty]['id']
  17.             new_response['users'][-1]['specialty_ids'][-1]['title'] = specialties[specialty]['title']
  18.     return new_response
  19.  
  20.  
  21. response = {
  22.     'users': [
  23.         {
  24.             'id': 129,
  25.             'surname': 'Петров',
  26.             'specialty_ids': [
  27.                 63
  28.             ],
  29.             'avatar_id': None
  30.         }
  31.     ],
  32.     'specialties': [
  33.         {
  34.             'id': 63,
  35.             'title': 'Программист'
  36.         }
  37.     ]
  38. }
  39.  
  40. print(beautify_response(response))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement