简体   繁体   中英

how to remove brackets from an inserted tuple in a list?

please can somebody help me remove the extra brackets from my tuple list?

latest_Value = {"17:00:00": 100.00}

Dict1 = {"current value": 100, "stock_purchased": "false", "Historic value": [('16:00:00', 55.50), ("15:00:00", 45.50), ("14:00:00", 75.50),("13:00:00", 65.50), ("12:00:00", 55.50)]}

# converting the latest_value into a tuple
List_it = [(k, v) for k, v in latest_Value.items()]

# insert the tuple into the tuple list
Dict1['Historic value'].insert(0, List_it)

data2 = Dict1["Historic value"]
print(data2)

#output 

[[('17:00:00', 100.0)], ('16:00:00', 55.5), ('15:00:00', 45.5), ('14:00:00', 75.5), ('13:00:00', 65.5), ('12:00:00', 55.5)]

when the list is ammended, it adds a nested list instead of just the tuple. How do you avoid this?

Kind Regards,

Andrew

# converting the latest_value into a tuple
List_it = [(k, v) for k, v in latest_Value.items()]

That is, in fact, wrong. This converts the entire dict to tuples, not just the last key-value pair so Dict1['Historic value'].insert(0, List_it) adds the entire list of tuples to Dict1['Historic value'] .

The fact that latest_Value contains a single key-value pair does not change the fact that List_it will be a list of tuples.

If you change to Dict1['Historic value'].insert(0, List_it[0]) then you will get the output you want.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM