I am getting this kind of list from mongodb in python. list1 = [{'a':'1'},{'a':'2'}] I wanted to get the values of the data from this list. when i tried print(a[0]) this gave me {'a':'1'} I just wanted to get the numerical values of this list to perform other maths operations on it. but i am unable to get the numerical values. What should i do?
Asked
Active
Viewed 166 times
3 Answers
3
you have to iterate over list element and get the vlaue from the dictionary.
eg
new_list1 = list(map(lambda x:x['a'], list1))
or using list comprehension, as suggested by @schwobaseggl
new_list1 = [i.get('a', None) for i in list1]

sahasrara62
- 10,069
- 3
- 29
- 44
-
if this solution help you fully, then you can accept this one as correct solution – sahasrara62 Jan 28 '21 at 11:54