0

I want to add the string " Equity" to each item in a list if the third last character in the item is " ". Otherwise, I would like to add " Index". Ideally, I don't want to use loops if there is a way.

For Example, I want the list below

list = ['VOD LN','HSBA LN', 'DOKA SS','SXNE' 'KERIN FH','YORK GY','SXNP']

to look like the list below

list = ['VOD LN Equity','HSBA LN Equity', 'DOKA SS Equity','SFNE Index' 'KERIN FH Equity','YORK GY','SXKP Index']

Any idea how i can do this?

Thanks!

5
  • There is no way to do this with no loop. They question is whether it will be explicit (list comprehension) or implicit (if abusing pandas) Commented Dec 30, 2019 at 14:02
  • Is the length of all strings in the list at least 3? If not, solutions based on s[-3] will throw run-time errors on occasion. Commented Dec 30, 2019 at 14:04
  • DeepSpace - I pretty sure there might be a way to create a function and then apply that function to the list, but I just don't know how John Coleman - yeah they will be at least 4 Commented Dec 30, 2019 at 14:06
  • @pythonlearner13 But the loop will still have to be somewhere, in your code or in a framework/library code. That's exactly what I meant by explicit/implicit loop Commented Dec 30, 2019 at 14:10
  • @JohnColeman Then use s[-3:-2] Commented Dec 30, 2019 at 14:12

3 Answers 3

2

You can do this with a list comprehension containing a conditional expression.

>>> input_list = ['VOD LN','HSBA LN', 'DOKA SS','SXNE', 'KERIN FH','YORK GY','SXNP']
>>> result = [x + (' Equity' if len(x)>=3 and x[-3]==' ' else ' Index') for x in input_list]
>>> result
['VOD LN Equity', 'HSBA LN Equity', 'DOKA SS Equity', 'SXNE Index', 'KERIN FH Equity', 'YORK GY Equity', 'SXNP Index']

You could also use various string formatting operations, but since you're just concatenating two strings for each item, + works fine.

Sign up to request clarification or add additional context in comments.

Comments

2

Seems it can help you:

items = ['VOD LN','HSBA LN', 'DOKA SS','SXNE' 'KERIN FH','YORK GY','SXNP']

def update(item: str) -> str:
    if len(item) > 3 and item[-3] == ' ':
        return item + ' Equity'
    return item + ' Index'

updated_items = [
    update(item) for item in items
]

2 Comments

I think this is the best answer since it does a safety check on the length of the strings. Even if not needed in OP's case (they didn't specify), defensive programming is good in the long run. An explicit loop which handles edge cases is preferable to a one-liner that might fail.
@JohnColeman A working one liner can be achieved with s[-3:-2] or the length check can be done inside the comprehension like in khelwood's answer
2

One line solution

words = ['VOD LN', 'HSBA LN', 'DOKA SS', 'SXNE' 'KERIN FH', 'YORK GY', 'SXNP']
words = [f'{word} Equity' if word[-3] == ' ' else f'{word} Index' for word in words]
print(words) # ['VOD LN Equity', 'HSBA LN Equity', 'DOKA SS Equity', 'SXNEKERIN FH Equity', 'YORK GY Equity', 'SXNP Index']

If you use Python version older then 3.6

words = [word + ' Equity' if word[-3] == ' ' else word + ' Index' for word in words]

*don't use list as variable name, it's a built in type.

6 Comments

... else f'{word} Index' ...
@DeepSpace my bad, fixed.
I get the following error File "<ipython-input-48-1d20f3955ca7>", line 2 words = [f'{word} Equity' if word[-3] == ' ' else f'{word} Index' for word in words] ^ SyntaxError: invalid syntax
@pythonlearner13 you can't go over all the items i a list without using a loop or an equivalent of a loop.
the version is 3.5.5
|

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.