0

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:

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.

1
  • 1
    list1_altered = list1[0].split(', ')? It seems that list1 is a list containing exactly one element, which is a string, so to produce the output you want you just need to first get that element (list1[0]) and then split it on a comma. Commented Dec 8, 2021 at 5:12

1 Answer 1

1

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(',')
Sign up to request clarification or add additional context in comments.

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.