I'm trying to loop over this list:
data = [
{
"date":"2022-09-02 08:00:00.000",
"endtime":"2022-09-02 12:00:00.000",
"timebilled":"4.00",
"projectno":"2479"
},
{
"date":"2022-09-02 13:00:00.000",
"endtime":"2022-09-02 16:00:00.000",
"timebilled":"3.00",
"projectno":"2696"
},
{
"date":"2022-09-02 16:00:00.000",
"endtime":"2022-09-02 17:00:00.000",
"timebilled":"1.00",
"projectno":"2479"
},
{
"date":"2022-09-01 08:00:00.000",
"endtime":"2022-09-01 10:00:00.000",
"timebilled":"2.00",
"projectno":"2696"
},
{
"date":"2022-09-01 10:00:00.000",
"endtime":"2022-09-01 12:00:00.000",
"timebilled":"2.00",
"projectno":"2479"
},
{
"date":"2022-09-01 13:00:00.000",
"endtime":"2022-09-01 17:00:00.000",
"timebilled":"4.00",
"projectno":"2479"
},
{
"date":"2022-08-31 08:00:00.000",
"endtime":"2022-08-31 16:00:00.000",
"timebilled":"8.00",
"projectno":"2489"
},
...
]
and condense the info to make it look like this:
projectHours = {
{ "2696":[
{
"2022-09-02 13:00:00.000":"3.00"
},
{
"2022-09-01 10:00:00.000":"2.00"
}
]
}, {
"2479":[
{
"2022-09-02 08:00:00.000":"4.00"
},
{
"2022-09-02 16:00:00.000":"1.00"
},
{
"2022-09-01 10:00:00.00":"2.00"
},
{
"2022-09-01 13:00:00.000":"4.00"
}
]
}, {
"2489":[
{
"2022-08-31 08:00:00.000":"8.00"
}
]
}
}
so far this is what I have:
info = {}
batch = {}
projectNums = ['2479', '2696', '2489']
for element in data:
for projNum in projectNums:
if element["projectno"] == projNum:
startDate = element["date"]
info = {
startDate: element["timebilled"]
}
batch.append(info)
project = {
projNum: batch
}
print(project)
this is my result:
{
"2696":[
{
"2022-09-02 13:00:00.000":"3.00"
}
]
}, {
"2696":[
{
"2022-09-02 13:00:00.000":"3.00"
}
{
"2022-09-01 08:00:00.000":"2.00"
}
]
}, {
"2489":[
{
"2022-08-31 08:00:00.000":"8.00"
}
]
}, {
"2479":[
{
"2022-09-02 08:00:00.000":"4.00"
}
]
}, {
"2479":[
{
"2022-09-02 08:00:00.000":"4.00"
},
{
"2022-09-02 16:00:00.000":"1.00"
}
]
}, {
"2479":[
{
"2022-09-02 08:00:00.000":"4.00"
},
{
"2022-09-02 16:00:00.000":"1.00"
},
{
"2022-09-01 08:00:00.000":"2.00"
}
]
}, {
"2479":[
{
"2022-09-02 08:00:00.000":"4.00"
},
{
"2022-09-02 16:00:00.000":"1.00"
},
{
"2022-09-01 08:00:00.000":"2.00"
},
{
"2022-09-01 13:00:00.000":"4.00"
}
]
}
I'd like to remove the previous key-value pairs and replace them with the most recent ones. How would I be able to do that? I've tried changing the type of collection but did not work for me. I'm pretty new to Python and any help would be appreciated.
Thank you!