Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Issue in getting the correct list items/values using python

I have a list in python like this:

list1 = ['Security Name % to Net Assets* DEBENtURES 0.04, Britannia Industries Ltd. EQUity & RELAtED 96.83, HDFC Bank 6.98, ICICI 4.82']

if I get the length of this list using len(list1) then it gives as 1 simply because it considers all as 1.

How can I transform my list1 such that it would look like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

list1_altered = ['Security Name % to Net Assets* DEBENtURES 0.04', 'Britannia Industries Ltd. EQUity & RELAtED 96.83', 'HDFC Bank 6.98', 'ICICI 4.82']

after which upon using len(list1_altered) I should be able to get value as 4

I have tried using replace(",", "\',\'") however it doesn’t give the desired result.

Please help me do this.

>Solution :

Replacing the commas by quotes is not going to change the fact that you’ll still have a single string. You won’t transform an object (string) into another (list of strings) this way.

Use str.split to generate a list of multiple strings split on a separator:

list1_altered = list1[0].split(',')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading