I am new to this community, I have tried this below code, this works fine, but looking for a better way of approaching for performance.

Please edit my question, it it is not understandable.

I have a string and list, I need to filter that and converting that string to a dictionary , then if a dictionary key presents in the list, we need to append the corresponding dict[value] to my list

text='p.O(post office)\nR.J(Radio Jocky)'
for item in text_splitted:
list2=[my_dict[item] for item in my_dict.keys() if item in list1]
list1=['R.J', 'Radio Jocky']
  • \$\begingroup\$ What is your reasoning to convert it to a dictionary first, and afterwards convert it back to a list? \$\endgroup\$ – Ludisposed Nov 22 '17 at 10:01
  • \$\begingroup\$ Are all values in the text like this name(job)\n...? \$\endgroup\$ – Ludisposed Nov 22 '17 at 10:02
  • \$\begingroup\$ @Ludisposed , I a value present list1, i need to append the corresponding meaning to my list1, you can see my final output R.J is already present in list1' so im adding the meaning of R.J` in the same list. To get this result im converting to dictionary and extracting the values. If there is any easy solution, please answer it \$\endgroup\$ – pyd Nov 22 '17 at 10:06
  • \$\begingroup\$ some values will have two parameters like name(job,time) \$\endgroup\$ – pyd Nov 22 '17 at 10:08
  • \$\begingroup\$ Please change the question title, 'how to' questions are off-topic here. And so shouldn't be a question title. \$\endgroup\$ – Peilonrayz Nov 22 '17 at 10:50
  • I think the step of converting it to a list is unnecesarry, because we can check if it is in the list the same.
  • I have a solution using regex to find the corresponding name and job
  • You could use better variable names because item and list1 list2 are nondescriptive

Note that using wilcards .* would be considered bad form in a regex

You might want to change that to suit your needs.

from re import findall

def add_job(L, text):
    for line in text.split('\n'):
        name, job = findall(r"(.*)\((.*\))", line)[0]
        if name in L:
    return L

if __name__ == '__main__':
    text='p.O(post office)\nR.J(Radio Jocky)'
    list1 = add_job(list1, text)
  • \$\begingroup\$ Thanks for the solution, can you tell me the best resource to learn regex ? \$\endgroup\$ – pyd Nov 22 '17 at 10:15
  • 1
    \$\begingroup\$ regexone.com to learn and regex101.com to check your regular expressions \$\endgroup\$ – Ludisposed Nov 22 '17 at 10:20
  • \$\begingroup\$ @pyd I know you are new here, my solution is far from optimal (I think) and you could maybe have a better one if you didnt accept on first sight. Codereview works a bit different then StackOverflow and I suggest to read this meta post. And more on the meta to see the guidelines of Codereview. Have fun ^^ \$\endgroup\$ – Ludisposed Nov 22 '17 at 10:24
  • \$\begingroup\$ okay, I will wait for many solutions, from next question onward :D \$\endgroup\$ – pyd Nov 22 '17 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.