So, I have a list which contains a strings inside strings and I want to convert it to normal list of strings.
I have this :
["'NSE:NIFTY2311218050CE'", " 'NSE:NIFTY2311218050PE'", " 'NSE:NIFTY2311218100CE'", " 'NSE:NIFTY2311218100PE'", " 'NSE:NIFTY2311218150CE'", " 'NSE:NIFTY2311218150PE'", " 'NSE:NIFTY2311218200CE'", " 'NSE:NIFTY2311218200PE'", " 'NSE:NIFTY2311218250CE'", " 'NSE:NIFTY2311218250PE'", " 'NSE:NIFTY2311218300CE'", " 'NSE:NIFTY2311218300PE'", " 'NSE:NIFTY2311218350CE'", " 'NSE:NIFTY2311218350PE'", " 'NSE:NIFTY2311218400CE'", " 'NSE:NIFTY2311218400PE'"]
I want something just like this:
['NSE:NIFTY2311218050CE', 'NSE:NIFTY2311218050PE', 'NSE:NIFTY2311218100CE']
CodePudding user response:
Try to replace single quote then space it will work.
l=["'NSE:NIFTY2311218050CE'", " 'NSE:NIFTY2311218050PE'", " 'NSE:NIFTY2311218100CE'", " 'NSE:NIFTY2311218100PE'", " 'NSE:NIFTY2311218150CE'", " 'NSE:NIFTY2311218150PE'", " 'NSE:NIFTY2311218200CE'", " 'NSE:NIFTY2311218200PE'", " 'NSE:NIFTY2311218250CE'", " 'NSE:NIFTY2311218250PE'", " 'NSE:NIFTY2311218300CE'", " 'NSE:NIFTY2311218300PE'", " 'NSE:NIFTY2311218350CE'", " 'NSE:NIFTY2311218350PE'", " 'NSE:NIFTY2311218400CE'", " 'NSE:NIFTY2311218400PE'"]
l1=[]
for s in l:
s1=s.replace("'", "")
l1.append(s1.replace(" ", ""))
l1
CodePudding user response:
You need to strip
single quotes and spaces.
Assuming lst
the input list:
lst = [s.strip("' ") for s in lst]
Output:
['NSE:NIFTY2311218050CE', 'NSE:NIFTY2311218050PE',
'NSE:NIFTY2311218100CE', 'NSE:NIFTY2311218100PE',
'NSE:NIFTY2311218150CE', 'NSE:NIFTY2311218150PE',
'NSE:NIFTY2311218200CE', 'NSE:NIFTY2311218200PE',
'NSE:NIFTY2311218250CE', 'NSE:NIFTY2311218250PE',
'NSE:NIFTY2311218300CE', 'NSE:NIFTY2311218300PE',
'NSE:NIFTY2311218350CE', 'NSE:NIFTY2311218350PE',
'NSE:NIFTY2311218400CE', 'NSE:NIFTY2311218400PE']
If the input is a pandas Series (DataFrame column):
df['out'] = df['col'].str.strip("' ")