1

Hi I'm having a hard time iteration over an array of dictionaries and can't figure it out what's failing

This is the data:

total = [{'table_id': 'IA_AUTO_2020-11-25', 'created': datetime.datetime(2020, 11, 25, 5, 36, 1, 281000, tzinfo=datetime.timezone.utc)},
    {'table_id': 'IA_AUTO_2020-12-07', 'created': datetime.datetime(2020, 12, 7, 5, 55, 4, 142000, tzinfo=datetime.timezone.utc)},
    {'table_id': 'IA_AUTO_2020-12-09', 'created': datetime.datetime(2020, 12, 9, 5, 52, 55, 48000, tzinfo=datetime.timezone.utc)}]

And this is the iteration code:

n=[]
d=[]
for t in total:
    n.append(t.table_id)
    d.append(t.created)
    total.append(t.table_id)
    total.append(t.created)
print(n)

But I get the error:

`        for t in total:
            print(t)
>           n.append(t.table_id)

   for t in total('trending_CL'):
TypeError: 'list' object is not callable` 

An important note: I can't change the loop structure, but rater modify the data structure to make it work

I'm aware the issue must be rather simple, but really can't find it

  • Dictionaries use [subscription](https://docs.python.org/3/reference/expressions.html#subscriptions) to access their values - [https://docs.python.org/3/tutorial/datastructures.html#dictionaries](https://docs.python.org/3/tutorial/datastructures.html#dictionaries). – wwii Feb 09 '21 at 14:28
  • 1
    `t.table_id` should be `t['table_id']` – Tomerikoo Feb 09 '21 at 14:31
  • Please [edit] your question to include a [mre]. It is not clear what is `client.list_tables` here – Tomerikoo Feb 09 '21 at 14:58
  • @Tomerikoo I edited the question, since I was trying to mock the data – Juan Penaloza Feb 09 '21 at 15:23

2 Answers2

2

you are not calling a dict's attribute (for attributes it's correct to use obj.attr) but a dict's keys, in fact the items of a dict are NAMED BY KEY, so to access them you have to do dict["key"] and to modify them dict["key"] = "value"

so your code should be:

n=[]
d=[]
for t in client.list_tables('trending_CL'):
    n.append(t['table_id'])
    d.append(t['created'])
    total.append(t['table_id'])
    total.append(t['created'])
Leonardo Scotti
  • 1,069
  • 8
  • 21
1

You access the keys of a dictionary using my_dict['my_key'].

The following code should work:

n=[]
d=[]
for t in client.list_tables('trending_CL'):
    n.append(t['table_id'])
    d.append(t['created'])
    total.append(t['table_id'])
    total.append(t['created'])
arhr
  • 1,505
  • 8
  • 16
  • This throws an error : ´E NameError: name 'table_id' is not defined´ – Juan Penaloza Feb 09 '21 at 14:40
  • @JuanPenaloza that's not possible from the above code... – Tomerikoo Feb 09 '21 at 14:41
  • Have you correctly transcripted the `'` before and after `table_id`? In your code you are looking for the variable called `table_id` and not for the string `'table_id'` – arhr Feb 09 '21 at 14:42
  • @Tomerikoo when I execute this potion of code in a new clean file, I got ```for t in total('trending_CL'): TypeError: 'list' object is not callable``` What do you mean with "not possible"? – Juan Penaloza Feb 09 '21 at 14:51
  • @JuanPenaloza That's not the error you reported before. The one from before is not possible from the above code. The second error you mentioned is hard to say because we don't know what is `list_tables`. Post a [mre] in the question – Tomerikoo Feb 09 '21 at 14:57