I have a list of tuples containing data, and another corresponding tuple containing some header information. Based on the header, I would like to extract a specific value from a given element in the list. Example:
>>> hdr = ("a", "b", "c", "d", "e")
>>> elt = (1, 2, 3, 4, 5)
>>> my_func(elt, "c")
3
The key here is that I know the header names ahead of time but not their position in the header tuple. What is the easiest way to find the value in elt corresponding to "c" in hdr? I am using Python 3.2.
Easiest?
dict(zip(hdr, elt))["c"]
An alternative would be:
elt[hdr.index("c")]
However building a dict (as per the first suggestion) would be more efficient if you're making repeated searches.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With