-1

I have a list ListA:

ListA = [ 'Key1: test1',  'Key2: test2', 'Key3: test3']

What I'm trying to do is I want to search a certain value to it respective key. For example: If user input test1, it should return me Key1 OR If user input test3, it should return me Key3 . I research a lot on the internet but it seem many are talking about comparing the two different list but not comparing the same list. I'm still new to programming, so I want to ask does my idea are lousy? Is it a better way to do it ?

WEI ZHUANG GOH
  • 313
  • 1
  • 13
  • 5
    Your `DictA` is not a dict at all, it's a list of strings. – Thierry Lathuille Oct 26 '21 at 09:54
  • Keys will always be unique but values may be duplicates. what is the expectation if the user puts in a value which would map to multiple keys? – Chris Doyle Oct 26 '21 at 09:56
  • @ChrisDoyle In my case, user can only input 1 key at once. They're not allow to search multiple keys – WEI ZHUANG GOH Oct 26 '21 at 09:57
  • Where does your data in such a format come from? Couldn't you get it in a more useful format? – Thierry Lathuille Oct 26 '21 at 09:57
  • but what if you had `ListA = [ 'Key1: test1', 'Key2: test2', 'Key3: test2']` if the user inputs test2 what would be the expected output? since test2 maps to 2 different keys – Chris Doyle Oct 26 '21 at 09:58
  • @ChrisDoyle Cause the list will not have a duplicate value, everything is unique – WEI ZHUANG GOH Oct 26 '21 at 10:00
  • @ThierryLathuille Originally the key are retrieve from a json list `{'serial1': 'key1', 'subject1': 'test1', 'serial2': 'key2', 'subject2': 'test2', 'serial3': 'key3', 'subject3': 'test3'}` – WEI ZHUANG GOH Oct 26 '21 at 10:02
  • 1
    So just use the `json` module in order to get a real dict, and if you are sure that your values are unique, you can [invert it](https://stackoverflow.com/questions/483666/reverse-invert-a-dictionary-mapping) – Thierry Lathuille Oct 26 '21 at 10:05

1 Answers1

2

To solve the problem as originally presented:

from typing import Dict, List


ListA = ['Key1: test1',  'Key2: test2', 'Key3: test3']

def get_key_from_value_list(target_list: List, target_value: str):
    for entry in target_list:
        key, list_value = entry.split(":")
        if target_value == list_value.strip():
            return key

print("List:", get_key_from_value_list(ListA, "test1"))

Result:

List: Key1

But given that you mentioned you're deserialising JSON, you would be better off using the actual json module in Python to parse it so you get an actual dict object, which makes this much easier than dealing with a list. Doing so would then require:

from typing import Dict, List
import json

dict_a = json.loads('{"Key1": "test1","Key2": "test2","Key3": "test3"}')

def get_key_from_value_dict(target_dict: Dict, target_value: str):
    for key, value in target_dict.items():
        if target_value == value:
            return key

print("Dict: ", get_key_from_value_dict(dict_a, "test1"))

Result:

Dict:  Key1
Da Chucky
  • 781
  • 3
  • 13