-3
arrrSample =

 {'art_other_country_names': None, 
  'pk_ba_country_id': 186,
  'sin_active': 1,
  'txt_remarks': '',
  'vhr_common_country_name': 'Uzbekistan',
  'vhr_formal_country_name': 'Republic of Uzbekistan',
  'vhr_nationality': '',
  'vhr_short_country_name': '',
  'vhr_transaction_currency_code': 'UZS'},


 {'art_other_country_names': None,
  'pk_ba_country_id': 185,
  'sin_active': 1,
  'txt_remarks': '',
  'vhr_common_country_name': 'Uruguay',
  'vhr_formal_country_name': 'Oriental Republic of Uruguay',
  'vhr_nationality': '',
  'vhr_short_country_name': '',
  'vhr_transaction_currency_code': 'UYU'},


 {'art_other_country_names': None,
  'pk_ba_country_id': 184,
  'sin_active': 1,
  'txt_remarks': '',
  'vhr_common_country_name': 'United States',
  'vhr_formal_country_name': 'United States of America',
  'vhr_nationality': '',
  'vhr_short_country_name': '',
  'vhr_transaction_currency_code': 'USD'},

This is the sample of data i get from database. How do i find the index in a single statement?

Eg if the nationality of the third entry is updated i require something like this:

arrrSample[index]['vhr_nationality'] ="American"

How do I find the index? I have the value 'pk_ba_country_id': 184 with me which is arrrSample[2]['pk_ba_country_id'] =184.

How do I find this index 2 from 184 without looping?

I need to find index from the value, I do not wish to use a loop the array as the database can contain large data .

Jacques Gaudin
  • 15,779
  • 10
  • 54
  • 75
  • What you're defining there isn't a multidimensional array. You're assigning multiple dictionaries to one variable, which (I think) will result in a tuple. You'll probably need to explain what you're doing before anyone can help you. See [Python list of dictionaries search](https://stackoverflow.com/q/8653516/1288) for an example of how to define and search in a list of dictionaries. – Bill the Lizard May 07 '18 at 12:22
  • You do not have a multidimensional array, as it is normally understood in Python to mean a `numpy.ndarray`, or *perhaps* a nested list. Your example "array" is not an array, it would give a syntax error. Assuming you have a *list of dictionaries*, then there is no way to find that index without looping, even if it is at least once to build an map from values of interest to indices. – juanpa.arrivillaga May 07 '18 at 16:31

1 Answers1

0

I assume your target is something like this (arrrSample was not correctly formed)?

arrrSample =[{'art_other_country_names': None,'pk_ba_country_id': 186,
  'sin_active': 1,
  'txt_remarks': '',
  'vhr_common_country_name': 'Uzbekistan',
  'vhr_formal_country_name': 'Republic of Uzbekistan',
  'vhr_nationality': '',
  'vhr_short_country_name': '',
  'vhr_transaction_currency_code': 'UZS'},
 {'art_other_country_names': None,
  'pk_ba_country_id': 185,
  'sin_active': 1,
  'txt_remarks': '',
  'vhr_common_country_name': 'Uruguay',
  'vhr_formal_country_name': 'Oriental Republic of Uruguay',
  'vhr_nationality': '',
  'vhr_short_country_name': '',
  'vhr_transaction_currency_code': 'UYU'},
 {'art_other_country_names': None,
  'pk_ba_country_id': 184,
  'sin_active': 1,
  'txt_remarks': '',
  'vhr_common_country_name': 'United States',
  'vhr_formal_country_name': 'United States of America',
  'vhr_nationality': '',
  'vhr_short_country_name': '',
  'vhr_transaction_currency_code': 'USD'}]

for data in arrrSample:
     if data['pk_ba_country_id'] == 184:
        print(data['vhr_common_country_name'])
Mika72
  • 413
  • 2
  • 12