fcb434
fcb434

Reputation: 83

Replacing List value using dictionary that contains an underscore

I am trying to use a dictionary to replace usernames with their real names. For example, if the input is "bob2000", the dictionary will replace the list value as "Bob Doe".

However, I am getting an problem with a username with an underscore. The value does not get changed.

For example,

my_dict = {
     'bob2000':'Bob Doe',
     'bob_2001':'Bob Smith'}

Returns:

my_list = ['Bob Doe', 'bob_2001']

The value is simply getting skipped over. My code is shown below.

def name_replace(my_list):
    data = [my_dict.get(item,item)  for item in my_list]
return (data)

Upvotes: 1

Views: 58

Answers (1)

Jay Shankar Gupta
Jay Shankar Gupta

Reputation: 6088

From function name_replace return data not my_list

my_dict = {
     'bob2000':'Bob Doe',
     'bob_2001':'Bob Smith'}
my_list = ['Bob Doe', 'bob_2001']
def name_replace(my_list):
  data = [my_dict.get(item,item)  for item in my_list]
  return (data)
print(name_replace(my_list))

Output: ['Bob Doe', 'Bob Smith']

Upvotes: 2

Related Questions