Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete array values in python

Tags:

python

arrays

I have a simple question, if I have an array of strings in python: ['a', 'b', 'c', 'd'] is there a way that I can compare another string and if it exists in the array delete that value and everything after it? I'm new to python and I'm not too familiar with the syntax but pseudocode:

s = 'b'
array = ['a', 'b', 'c', 'd']

if b exists in array
    remove b and elements after

so the new array would simply be ['a']. Any help would be much appreciated!

like image 248
Ryan Sayles Avatar asked May 25 '26 15:05

Ryan Sayles


1 Answers

s = 'b'
array = ['a', 'b', 'c', 'd']

if s in array:
    del array[array.index(s):]
like image 193
wflynny Avatar answered May 28 '26 05:05

wflynny



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!