python - Avoid using two arrays to make a simple string change -
i've list json file names , want remove .json
words.
what i've done , works is:
jsonlist = ['foo.json', 'bar.json'] jsonlist_parsed = [] in jsonlist: x = i.replace(".json", "") jsonlist_parsed.append(x)
jsonlist_parsed
prints me desired solution, ['foo', 'bar']
is there way avoid using 2 arrays , doing change in jsonlist
array?
thanks in advance
you can use list comprehension, this
jsonlist = ['foo.json', 'bar.json'] print [item.replace(".json", "") item in jsonlist] # ['foo', 'bar']
Comments
Post a Comment