2

I have the following list :-

list= ['order','orderNumber']

I want enclose order in double_quotes "order" not orderNumber I tried the below in list comprehension

if 'order' in list:
     list = [item.replace('order', '"' + 'order' + '"') for item in list]

But the output is :- order from orderNumber is also getting enclosed with double quotes which is not needed

['"order"', '"order"Number']

Exepected Output is :-

['"order"', 'orderNumber']

Dot Net Dev 19
  • 345
  • 2
  • 12

3 Answers3

3

You should use == operator and not in as:

lst = ['order','orderNumber']
lst = [f'"{elt}"' if elt == 'order' else elt for elt in lst]
print(lst)

Also, don't use list for object names.


Update: Use concatenation operator + for Python version less that 3.6 that don't support fstrings.

lst = ['order','orderNumber']
lst = ['"{}"'.format(elt) if elt == 'order' else elt for elt in lst]
print(lst)
Krishna Chaurasia
  • 8,924
  • 6
  • 22
  • 35
  • thanks..works...f stands for replacement..? – Dot Net Dev 19 Feb 25 '21 at 13:45
  • f short for fstrings is used for formatting strings – Krishna Chaurasia Feb 25 '21 at 13:46
  • works with python3...but for python 2.7 doesnot support F prefix ...is there an alternative apart from using regex – Dot Net Dev 19 Feb 25 '21 at 13:55
  • 1
    added the code with contenation operator `+` without fstrings. – Krishna Chaurasia Feb 25 '21 at 14:04
  • 1
    @KrishnaChaurasia: I'd suggest either just using `'"order"'` (since it's literally that one string we're handling, we don't need to work in terms of the loop variable), or if you hate repeating yourself (wouldn't blame you), `'"{}"'.format(elt)`, for a solution that works back to 2.6 (creates output directly, similar to the f-string, rather than producing a temporary intermediate string; `str.format` is the predecessor to f-strings, that are a more powerful version of `str.format` supported directly by language syntax). Pre-2.6, `'"%s"' % elt` (`'"%s"' % (elt,)` when `elt` might be a `tuple`). – ShadowRanger Feb 25 '21 at 15:06
  • Thanks for your detailed comment @ShadowRanger. I have updated the answer with using `format()`. – Krishna Chaurasia Feb 26 '21 at 06:43
2

You could use a regular expression and re.sub:

>>> import re
>>> lst = ['order', 'orderNumber']
>>> [re.sub('^order$', '"order"', item) for item in lst]
>>> ['"order"', 'orderNumber']
yvesonline
  • 4,609
  • 2
  • 21
  • 32
0

Could you please check if the below works for you

lista = ['order','orderNumber']
listb = []
if 'order' in lista:
    for item in lista:
        if item == 'order':
            listb.append(item.replace('order', '"' + 'order' + '"'))
        else:
            listb.append(item)
lista = listb
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
Arun R G
  • 16
  • 3
  • 1) Code-only answers are generally frowned on, especially when they're making huge changes to the original code and the relevance of any given change is unclear. 2) This is wildly overcomplicating things (e.g. when you know, for a fact, the string is just `'order'`, you nevertheless rely on `str.replace` to make an updated version, when you could just do `listb.append('"order"')`. – ShadowRanger Feb 25 '21 at 15:13
  • 3) With a listcomp, making a new `list` is unavoidable, but when the intent is to update in place, as you (sort of) do when you reassign `lista` at the end, you don't need to build a new `list`; `for i, item in enumerate(lista): if item == 'order': lista[i] = '"order"'` would avoid the need to build a new `list`, and keep the work to near-zero for all the elements that are left unchanged. And on thinking of this, I checked for duplicates of this question, and lo and behold, [the accepted answer on the duplicate](https://stackoverflow.com/a/2582183/364696) does this. :-) – ShadowRanger Feb 25 '21 at 15:14