4

example:

my list is ['tree','world','tre','worl']

my dict is {'tre':'good','worl':nice}

my scripts:

def replace(list, dictionary):
    for i in list:
        for k in dictionary:
            list = list.replace(k, dictionary[k])
    return list
print replace(input_file,curaw_dict)

but every time I receive the result is like:

goode
niced
good
nice

how can I make it more accurate make it like

tree
world
good
nice

Thanks alot

3
  • Don't use list as a variable name. It sqashed the function of the same name Commented Jul 20, 2017 at 4:35
  • 1
    I don't believe that you've run the code you've entered. list has no method called replace. Please create the shortest possible program that accurately demonstrates the error and copy-paste (never retype) that program into your question. See minimal reproducible example for more information. Commented Jul 20, 2017 at 4:40
  • What are those commas in the first list? Commented Jul 20, 2017 at 4:50

6 Answers 6

6

Lets make it a list comprehension instead.

replaced_list = [x if x not in my_dict else my_dict[x] for x in my_list]

I guess if you want a function you could do:

replace = lambda my_dict, my_list: [x if x not in my_dict else my_dict[x] for x in my_list]

or

def replace(my_list, my_dict):
    return [x if x not in my_dict else my_dict[x] for x in my_list]
Sign up to request clarification or add additional context in comments.

Comments

3
input_file = ['tree', 'world', 'tre', 'worl']
curaw_dict = {'tre':'good','worl':'nice'}

def replace(list, dictionary):
    return [curaw_dict.get(item, item) for item in list]
print replace(input_file,curaw_dict)

Comments

1
>>> li=['tree', 'world', 'tre', 'worl']
>>> di={'tre':'good','worl':'nice'}
>>> print('\n'.join(di.get(e,e) for e in li))
tree
world
good
nice

1 Comment

Thank you for this code snippet, which may provide some immediate help. A proper explanation would greatly improve its educational value by showing why this is a good solution to the problem, and would make it more useful to future readers with similar, but not identical, questions. Please edit your answer to add explanation, and give an indication of what limitations and assumptions apply.
0
def replace(list, dictionary):
    for idx, val in enumerate(list):
        if i in k:
           list[idx] = dictionary[list[idx]]
    return list
print replace(input_file,curaw_dict)

You don't need to iterate over a dictionary. Replace does partial replacements, but in will check if a key exists in a dictionary.

Comments

0

'key' in dictionary is the way to check if a key exists in a dict:

def replace(list, dictionary):
    new_list = []

    for i in list:
        if i in dictionary:
            new_list.append(dictionary[i])
        else:
          new_list.append(i)

    return new_list

Comments

0

for k in dictionary: is not the correct way to iterate over the items in the dictionary. Instead, you should use enumerate to iterate over the items in the list and look them up in the dictionary:

def replace(lst, dictionary):
    for k,v in enumerate(lst):
        if v in dictionary:
            lst[k] = dictionary[v]
    return lst

For each item in the list, k is the index of the value and v is the value itself. You then check if the value is in the dictionary, and if it is, you replace the value in the list with the value in the dictionary.

You also should not name your variables list, since that is a reserved word in Python.

You can alternatively use a list comprehension:

def replace(lst, dictionary):
    return [item if not item in dictionary else dictionary[item] for item in lst]

1 Comment

A few remarks: 1) list isn’t a reserved word. It’s just the class name for a built-in, and like any other name, it gets shadowed by other scopes. 2) for k in dictionary is a correct way to iterate over a dictionary; it’s just not the best one in this case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.