-1

I'm trying to append python list with two values returned as json within data into my final_list .

My code:

    data = json.loads(r.text)
    final_list = []
    for each_req in data:
        final_list.append(each_req(['requestId'],['author']))
    return final_list

Error:

TypeError: 'dict' object is not callable

If I try the same but only with requestId it works fine:

    data = json.loads(r.text)
    final_list = []
    for each_req in data:
        final_list.append(each_req['requestId'])
    return final_list

list then contains values like '12345' etc.

How can I append final_list to look like:

'12345 John Doe'

Dharman
  • 30,962
  • 25
  • 85
  • 135
Baobab1988
  • 685
  • 13
  • 33

4 Answers4

3

Do something like:

tup = (each_req['requestId'], each_req['author'])
final_list.append(tup)

each_req() implies that it's a function and/or callable in some way, which it's not, hence the error

C.Nivs
  • 12,353
  • 2
  • 19
  • 44
0

You need to access the dictionary again to append the value to the list.

Try this:

final_list.append(each_req['requestId'] + each_req['author'])
GhandiFloss
  • 392
  • 1
  • 6
0

You are not given what type of data is coming,assuming that your data is like data = {'key1':{'requestId':12345,'author':'Myname'}} for i in 'data' returns the 'keys' in dictionary and i['key1'] will not the value of that key

then try this code

def foo():
    data = {'key1':{'requestId':12345,'author':'Myname'}}
    final_list = []
    for each_req in data:
        final_list.append(f"{data[each_req]['requestId']} {data[each_req]['author']}")
    return final_list

print(foo())
Dharman
  • 30,962
  • 25
  • 85
  • 135
Arun K
  • 413
  • 4
  • 15
-2

Try:

final_list.append(each_req['requestId'], each_req['author'])
humanbeing
  • 81
  • 12