'Python split array and check the 0 index if the same and join it
I have an array looks like:
['varyasyon__in=27', 'varyasyon__in=25', 'varyasyon__in=1']
and I want to split every value on = and join it to be a string like:
varyasyon__in=27,25,1
Solution 1:[1]
Can you try the following:
f"varyasyon__in={','.join([val.split('=')[1] for val in data])}"
Output:
varyasyon__in=27,25,1
Solution 2:[2]
?ommon solution with dictionary:
lst = ['varyasyon__in=27', 'varyasyon__in=25', 'varyasyon__in=1']
d = {}
for i in lst:
d.setdefault(i.split('=')[0], set()).add(i.split('=')[1])
print(d)
Output:
{'varyasyon__in': {'27', '25', '1'}}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jeril |
| Solution 2 |
