Home > Mobile >  Issue in getting the correct list items/values using python
Issue in getting the correct list items/values using python

Time:12-08

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.

CodePudding user response:

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(',')
  • Related