-2

I have a list of like below, some of them are prefixed with "abc_" and some of them are not.

What would be the effective way to prefix the ones that do not have the prefix?

(Basically, I need all of them to have the prefix "abc_")

my_list = ['abc_apple','abc_orange','cherry','abc_berry','banana']

Required output:

my_list = ['abc_apple','abc_orange','abc_cherry','abc_berry','abc_banana']

Is it Possible to do it using list comprehension?

Aaryan
  • 51
  • 7
  • Loop over your list and check if the string starts with your prefix, if not then set the element to have it – Steven Summers May 06 '22 at 06:09
  • 1
    Yes. It is possible to do with list comprehension. Have you tried? – SiHa May 06 '22 at 06:15
  • 1
    It's unrelated to what you're trying to do, but using `list` as the name of one of your own variables is a very bad idea, since it will shadow the name of the builtin `list` class, which might cause very confusing bugs later on. – Blckknght May 06 '22 at 06:22

4 Answers4

2

Don't name lists with a Python Keyword. list is a keyword in Python. You can use list comprehension to do it using .startswith():

list1 = ['abc_apple','abc_orange','cherry','abc_berry','banana']
list1 = ['abc_'+i if not i.startswith('abc_') else i for i in list1]
print(list1)

Output:

['abc_apple', 'abc_orange', 'abc_cherry', 'abc_berry', 'abc_banana']
Abhyuday Vaish
  • 2,357
  • 5
  • 11
  • 27
2

Just following if/else in a list comprehension you could do something like this:

my_list = ['abc_apple','abc_orange','cherry','abc_berry','banana']
my_list = [f"abc_{word}" if not word.startswith("abc_") else word for word in my_list]
print(my_list)

Output:

['abc_apple', 'abc_orange', 'abc_cherry', 'abc_berry', 'abc_banana']

Tzane
  • 2,752
  • 1
  • 10
  • 21
1
list = ['abc_apple','abc_orange','cherry','abc_berry','banana']
for i in range(len(list)):
    if 'abc_' in list[i]:
        pass
    else:
        list[i] = 'abc_' + list[i]
        
list

output:

['abc_apple', 'abc_orange', 'abc_cherry', 'abc_berry', 'abc_banana']

OR

list = ['abc_apple','abc_orange','cherry','abc_berry','banana']
for i in range(len(list)):
    if 'abc_' not in list[i]:
        list[i] = 'abc_' + list[i]
        
list

OR Better answer

list = ['abc_apple','abc_orange','cherry','abc_berry','banana']
for i in range(len(list)):
    if  list[i].startswith('abc_'):
        pass
    else:
        list[i] = 'abc_' + list[i]
        
list
Raj Patel
  • 154
  • 6
  • 2
    This could be improved by using `str.startswith` rather than the `in` operator, since you'd incorrectly fail to prefix something that had `abc_` somewhere other than the start of the string. I'd also suggest using `enumerate` for the loop, so you can get the index and the value at the same time, rather than needing to index for the value a few different times. – Blckknght May 06 '22 at 06:14
  • Yes that is true. but as he had given a sample of list I thought it can be fine. but I agree with your answer – Raj Patel May 06 '22 at 06:15
1

Try method map to make an iterator that computes the function using arguments from each of the iterables.

>>> lst = ['abc_apple','abc_orange','cherry','abc_berry','banana']
>>> result = list(map(lambda x:x if x.startswith('abc_') else 'abc_'+x, lst))
>>>
>>> result
['abc_apple', 'abc_orange', 'abc_cherry', 'abc_berry', 'abc_banana']
Jason Yang
  • 11,284
  • 2
  • 9
  • 23