how to remove key value pair in dictionary based on condition python
Question:
I have a dictionary like
{
'timestamp': 1677231711722,
'instrument_name': 'BTC-30JUN23-8000-P',
'index_price': 23845.81,
'direction': 'buy',
'amount': 27.0},
{
'timestamp': 1677231711722,
'instrument_name': 'BTC-30JUN23-8000-P',
'index_price': 23845.81,
'direction': 'buy',
'amount': 2.4},
{
'timestamp': 1677231702010,
'instrument_name': 'BTC-25FEB23-27000-C',
'index_price': 23844.34,
'direction': 'buy',
'amount': 0.1}
I have timestamp as
1677228369641
which is derived from
old_time= round((datetime.datetime.now(timezone.utc) - timedelta(hours=1)).timestamp()*1000)
I want to compare timestamp in dict and old_time and remove the keys and values of that dict which are lesser than that.
How can we do it without using for loop efficiently.
Answers:
Key-value pairs from the dictionary based on the condition of timestamp being less than old_time
old_time = round((datetime.datetime.now(timezone.utc) - timedelta(hours=1)).timestamp() * 1000)
# given dictionary
data = [ { 'timestamp': 1677231711722, 'instrument_name': 'BTC-30JUN23-8000-P', 'index_price': 23845.81, 'direction': 'buy', 'amount': 27.0 }, { 'timestamp': 1677231711722, 'instrument_name': 'BTC-30JUN23-8000-P', 'index_price': 23845.81, 'direction': 'buy', 'amount': 2.4 }, { 'timestamp': 1677231702010, 'instrument_name': 'BTC-25FEB23-27000-C', 'index_price': 23844.34, 'direction': 'buy', 'amount': 0.1 }]
# dictionary comprehension to filter based on timestamp
filtered_data = [d for d in data if d['timestamp'] >= old_time]
# print filtered data
print(filtered_data)
I have a dictionary like
{
'timestamp': 1677231711722,
'instrument_name': 'BTC-30JUN23-8000-P',
'index_price': 23845.81,
'direction': 'buy',
'amount': 27.0},
{
'timestamp': 1677231711722,
'instrument_name': 'BTC-30JUN23-8000-P',
'index_price': 23845.81,
'direction': 'buy',
'amount': 2.4},
{
'timestamp': 1677231702010,
'instrument_name': 'BTC-25FEB23-27000-C',
'index_price': 23844.34,
'direction': 'buy',
'amount': 0.1}
I have timestamp as
1677228369641
which is derived from
old_time= round((datetime.datetime.now(timezone.utc) - timedelta(hours=1)).timestamp()*1000)
I want to compare timestamp in dict and old_time and remove the keys and values of that dict which are lesser than that.
How can we do it without using for loop efficiently.
Key-value pairs from the dictionary based on the condition of timestamp being less than old_time
old_time = round((datetime.datetime.now(timezone.utc) - timedelta(hours=1)).timestamp() * 1000)
# given dictionary
data = [ { 'timestamp': 1677231711722, 'instrument_name': 'BTC-30JUN23-8000-P', 'index_price': 23845.81, 'direction': 'buy', 'amount': 27.0 }, { 'timestamp': 1677231711722, 'instrument_name': 'BTC-30JUN23-8000-P', 'index_price': 23845.81, 'direction': 'buy', 'amount': 2.4 }, { 'timestamp': 1677231702010, 'instrument_name': 'BTC-25FEB23-27000-C', 'index_price': 23844.34, 'direction': 'buy', 'amount': 0.1 }]
# dictionary comprehension to filter based on timestamp
filtered_data = [d for d in data if d['timestamp'] >= old_time]
# print filtered data
print(filtered_data)