0
staff_text=['31','32']
staffing_title = ['14','28','14','20']

I have two array like above.and i want output like

staffing_title = ['31','28','32','20']

So basically whenever 14 comes in staffing_title array it replace by staff_text values.

ex if first 14 comes replace by 31,When second 14 comes replace by 32 and so on

2 Answers 2

1

Here is the one liner using list comprehension :

>>> staffing_title = ['14', '28', '14', '20']
>>> staff_text=['31','32']

>>> res = [staff_text.pop(0) if item == str(14) else item for item in staffing_title ]
>>> print(res)
['31', '28', '32', '20']   
Sign up to request clarification or add additional context in comments.

2 Comments

I want the output value inside array
I have also added one liner ( pythonic way) , @VivekSingh Please check
0

The following will do it:

>>> [t if t != '14' else staff_text.pop() for t in staffing_title]
['32', '28', '31', '20']

Note that this modifies staff_text, so you might want to make it operate on a copy.

This code assumes that there are at least as many elements in staff_text as there are '14' strings in staffing_title (but then you don't specify what should happen if there aren't).

Comments

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.