2

I have a following problem. I would like to form a list a conditionally.

Lets say I have a variable add_string and if it's True then

a = ["a","b","Added String","c"]

Else

a = ["a","b","c"]

what's the best way to do that? I can do that in the following manner

a = ["a","b","c"]
if add_string:
 a.insert(2,"Added String")

But that's not ideal since list a might change in future and I will have to change the index in the insert function. Also I have a condition — this added string should always follow after "b". Another solution is to search for "b" in the list and then insert after that, but that adds complexity and it's ugly.

Ideally I thought it should be something like

a = ["a","b",if add_string then "Added String","c"]
martineau
124k29 gold badges181 silver badges319 bronze badges
asked Jan 23, 2020 at 23:20
2
  • 4
    This sounds like an XY problem. What are you trying to do that you think warrants this approach? Commented Jan 23, 2020 at 23:23
  • it's exactly what I'm trying to achieve. Of course the list names and values are different. But basically I'm trying to form a list of objects in the function X. And this function is called from another function Y. One of the arguments that's passed is that object that I have to add to the list. Sometimes this objects is passed and sometimes not. Currently I'm doing it using insert. So I'm wondering if I can do it somehow else. Commented Jan 23, 2020 at 23:31

3 Answers 3

3
a = ["a","b"] + (["Added String"] if add_string else []) + ["c"]
answered Jan 23, 2020 at 23:31
Sign up to request clarification or add additional context in comments.

Comments

3

If you know all the values when you create a, you could do something like this:

add_string = True
a = ['a', 'b'] + (['Added String'] if add_string else []) + ['c']

Output:

['a', 'b', 'Added String', 'c']

If you don't know the values in a, you could use index to find the location of 'b' in a, and insert the string after that:

a = ["a","b","c"]
add_string = True
if add_string:
 a.insert(a.index("b")+1,"Added String")
print(a)

Output:

['a', 'b', 'Added String', 'c']
answered Jan 23, 2020 at 23:22

4 Comments

I know that I can do that and I mentioned that in the post "Another solution is to search for "b" in the list and then insert after that, but that adds complexity and it's ugly"
@Roman_T Do you know all the values in a when you create the list?
for now I know. But that list can change in future and new values can be added to the beginning of the list which will affect the index
@Roman_T if you are adding values to the list it's hard to see how you can do this without index...
1

You could set unwanted values to a known value (such as None) and then remove them using list comprehension:

add_string = False # Could be True
unfiltered_list = ["a","b","Added String" if add_string else None,"c"]
a = [x for x in unfiltered_list if x is not None]
print(a)
answered Jan 23, 2020 at 23:27

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.