-1

I have the list below that looks like this

my_test_data = []

when I print my_test_data I get the result below

[
    {
        'tnType': None, 
        'tnCode': None, 
        'postedDt': None, 
        'amt': None, 'tranDesc1': None
    }
]

This to me looks like a dictionary in a list

I tried printing like this

for key, val in my_test_data.items():
    print(key, val)

I got an error message like this : AttributeError: 'list' object has no attribute 'items'

I want to be able to read the value on every row and store that value in another list.

How can I read this one by one?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Baba
  • 2,059
  • 8
  • 48
  • 81
  • I'm not sure what you're confused about here, but I suppose I have the benefit of experience. The error message is telling you exactly what the problem is: `my_test_data` is a `list`, which doesn't have an `items` attribute. It's actually `dict` objects that have `items`. Did you forget to loop over `my_test_data`, or did you forget to select the first item (`my_test_data[0]`)? Or something else, like you expected the attribute access to be applied to all dicts inside the list? You can [edit] to clarify. – wjandrea Aug 17 '23 at 22:30
  • BTW, what interpreter are you using that prints like that? IPython also does pretty-printing, but in a different format. And I've never seen two keys printed on the same row while all other keys get their own row. – wjandrea Aug 17 '23 at 22:34
  • What should the other list look like at the end? It's hard to answer without knowing quite what you want. For reference, see [mre]. Please [edit] to clarify. – wjandrea Aug 17 '23 at 22:38
  • The other list should just store the values retrievec from each row. Example secondList = ['None', 'None', 'None'] – Baba Aug 17 '23 at 22:42
  • Why only three of them? Why are they strings? btw please [edit] to add that info. – wjandrea Aug 17 '23 at 22:44
  • Adding 3 was just an example to show you what it will look like. The count will match what is in the data. – Baba Aug 17 '23 at 22:46
  • Gotcha. Please write out *exactly* what you want to make it clear. – wjandrea Aug 17 '23 at 22:47
  • I need only the values stored in another list and this line of code will do it for me. I just tested it. for key, val in allTransactions[0].items(): print(val) – Baba Aug 17 '23 at 22:50

2 Answers2

1

Since this is an internal object inside a list, you can solve the problem with a simple approach.

You can access the list item at index 0 easily:

for key, val in my_test_data[0].items():
    print(key, val)

In this way, the objects inside the sub-object will be printed

outcome:

tnType None
tnCode None
postedDt None
amt None
tranDesc1 None

The easy way to insert the dictionary values ​​into a new list is like this:

values_list = []

for item in my_test_data:
    for key, val in item.items():
        values_list.append(val)

outcome:

[None, None, None, None, None]
NH.LOCAL
  • 21
  • 7
1

look you have some dictonary in your list right?
so in my code when you use for data in my_datas you create a loop for every item in your list
then when you use for items in data.items() every dictionary in your list you have run this code and you get the items and values!
i hope my explain was a good explain (my english level is bad sorry!)

my_datas = [
        {
            'tnType': None,
            'tnCode': None,
            'postedDt': None,
            'amt': None, 'tranDesc1': None
        }
    ]


for data in my_datas:
    for item in data.items():
        print(item)

the output :

('tnType', None)
('tnCode', None)   
('postedDt', None) 
('amt', None)      
('tranDesc1', None)
moonboy
  • 36
  • 4
  • This code looks good, but how does it answer the question? Please [edit] to add an explanation -- could be as little as one or two sentences. BTW, welcome to Stack Overflow! Check out the [tour], and [answer] if you want tips. – wjandrea Aug 17 '23 at 22:24
  • I want to be able to read the values of "None" and store all those None in another list. How can I get those values of None for each row? – Baba Aug 17 '23 at 22:39
  • Do you want the values (all of which are `None` at the moment in a list, or do you want the keys in a list where the value associated with the key is `None`? for eample a list like `["tnType", "tnCode", ...]` – JonSG Aug 17 '23 at 22:53
  • I need all the values stored in a list and counted. I think I have code to that that now. for key, val in my_test_data[0].items(): print(val). This will do it – Baba Aug 17 '23 at 22:56