So I have a dictionary with data
dict = {1:["code: 1", "date: 01-01-2000", "date: 01-02-2000", "date: 01-03-2000", "spent: 20$"]}
I want to get this output:
{1:["code: 1", "date: 01-01-2000", "spent: 20$"]}
So basically I only want the first element that have "date:" and the rest of elements
How can i obtain this?
CodePudding user response:
d = {1:["code: 1", "date: 01-01-2000", "date: 01-02-2000", "date: 01-03-2000", "spent: 20$"]}
a=[]
x=d[1][0:2]
y=d[1][-1]
a.append(x)
x.append(y)
output={1:x}
print(output)
CodePudding user response:
Try:
dct = {
1: [
"code: 1",
"date: 01-01-2000",
"date: 01-02-2000",
"date: 01-03-2000",
"spent: 20$",
]
}
out = {}
for k, v in dct.items():
tmp = {}
for i in reversed(v):
tmp[i.split(":")[0]] = i
out[k] = list(reversed(tmp.values()))
print(out)
Prints:
{1: ['code: 1', 'date: 01-01-2000', 'spent: 20$']}