Ani
Ani

Reputation: 1018

editing List content in Python

I have a variable data:

data = [b'script', b'-compiler', b'123cds', b'-algo', b'timing']

I need to convert it to remove all occurrence of "b" in the list. How can i do that?

Upvotes: 1

Views: 181

Answers (3)

Artsiom Rudzenka
Artsiom Rudzenka

Reputation: 29093

Not sure whether it would help - but it works with your sample:

initList = [b'script', b'-compiler', b'123cds', b'-algo', b'timing']
resultList = [str(x) for x in initList ]

Or in P3:

resultList = [x.decode("utf-8") for x in initList ] # where utf-8 is encoding used

Check more on decode function.

Also you may want to take a look into the following related SO thread.

Upvotes: 4

Dogbert
Dogbert

Reputation: 222108

>>> a = [b'script', b'-compiler', b'123cds', b'-algo', b'timing']
>>> map(str, a)
['script', '-compiler', '123cds', '-algo', 'timing']

Upvotes: 3

egor83
egor83

Reputation: 1237

strin = "[b'script', b'-compiler', b'123cds', b'-algo', b'timing']"
arr = strin.strip('[]').split(', ')
res = [part.strip("b'") for part in arr]

>>> res
['script', '-compiler', '123cds', '-algo', 'timing']

Upvotes: 1

Related Questions