-2

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?

Sunny
  • 15
  • 6

3 Answers3

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
0

Try print(int(list1[0]["a"]))

kennysliding
  • 2,783
  • 1
  • 10
  • 31
0
[v for d in list1 for (k,v) in d.items() ]

Output

['1', '2']
Ajay
  • 5,267
  • 2
  • 23
  • 30