Skip to content
Advertisement

using pop on multidimensional lists python with dynamoDB

I want to pop an item from a list of lists. So I have a scan for all dynamo Items and want to “pop” one field from each list.

For example:

 response = table.query(
            KeyConditionExpression=Key('userId').eq(userId)
        )
        
        agentList = response['Items']

The List:

  "result": [
    {
      "credentials": {
        "key": "xxx",
        "secret": "xxxx"
      },
      "active": true,
      "totalImported": "12345",
     }]

From this example, I have a bunch of Results and for every result list, I will remove the Item “credentials” like

agentList.pop('credentials')

However, this isn’t working

Advertisement

Answer

You’re doing the pop() on a list. So, you can specify a position in the list, but, as a list has no key values, you can’t use a string. Hence the error.

I’m not sure exactly what you’re trying to do, but, assuming you want to remove the ‘credentials’ from every item in the list you could do something like:

agentList = [
    {
        "credentials": {
            "key": "xxx",
            "secret": "xxxx"
        },
        "active": True,
        "totalImported": "12345",
    },
    {
        "credentials": {
            "key": "yyy",
            "secret": "yyy"
        },
        "active": True,
        "totalImported": "2222",
    }
]

for result in agentList:
    result.pop('credentials')

print(agentList)

Which would result in:

[{'active': True, 'totalImported': '12345'}, {'active': True, 'totalImported': '2222'}]
6 People found this is helpful
Advertisement