fetch unique values of item from dynamoDB using python

3k Views Asked by At

I am new to dynamoDB and unable to find material on how to fetch unique values of a particular item. I had a data in dynamoDB as show below:

Item_1      |  Item_2      |     Item_3
----------------------------------------
value_1_1   |  value_2_1   |    value_3_1
value_1_2   |  value_2_2   |    value_3_2
value_1_3   |  value_2_3   |    value_3_3
value_1_4   |  value_2_4   |    value_3_4

I want to get all the unique values of column Item_3

my approach is as follow:

import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('women_kurta')
i=1
pricelist=[]
while i<50:
    data = table.get_item(Key={'id':i})#   data.save()
    pricelist+=[data['Item']['price']]
    print i
    i+=1
print list(set(pricelist))

but it is very slow and not efficient. Is there any other way to do it efficiently?

1

There are 1 best solutions below

0
On BEST ANSWER

Untested code. So tweak it as necessary.

Instead of creating a new list every time, just append it. You will see many order of magnitude in speed.

pricelist.append(data['Item']['price'])

Use list comprehension for further speed improvement. This is just an idea. Try to understand and fix any syntax.

import boto3
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('women_kurta')
pricelist = [table.get_item(Key={'id':i})['Item']['price'] for i in xrange(50)]
print list(set(pricelist))