I have a dictionary inside of a list and I want to remove the square brackets and single quotes inside of them.
Current Output:
[{'label': "['Tennessee'] 22960 Packages", 'value': 'TN'},
{'label': "['Illinois'] 6277 Packages", 'value': 'IL'},
{'label': "['California'] 4 Packages", 'value': 'CA'},]
Desired Output:
[{'label': "Tennessee 22960 Packages", 'value': 'TN'},
{'label': "Illinois 6277 Packages", 'value': 'IL'},
{'label': "California 4 Packages", 'value': 'CA'},]
Try this with a for loop:
m[0]['label'] = m[0]['label'].replace("['", "").replace("']", "")
Result:
[{'label': 'Tennessee 22960 Packages', 'value': 'TN'},
{'label': "['Illinois'] 6277 Packages", 'value': 'IL'},
{'label': "['California'] 4 Packages", 'value': 'CA'}]
With for loop:
m = [{'label': "['Tennessee'] 22960 Packages", 'value': 'TN'},
{'label': "['Illinois'] 6277 Packages", 'value': 'IL'},
{'label': "['California'] 4 Packages", 'value': 'CA'},]
for i in range(0, 3):
m[i]['label'] = m[i]['label'].replace("['", "").replace("']", "")
print(m)
Try this:
def remover(x):
x['label'] = x['label'].replace("['", "").replace("']", "")
return x
mylist = [{'label': "['Tennessee'] 22960 Packages", 'value': 'TN'},
{'label': "['Illinois'] 6277 Packages", 'value': 'IL'},
{'label': "['California'] 4 Packages", 'value': 'CA'},]
mylist = list(map(remover,mylist))
print (mylist)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.