1

I have a tuple that looks like this:

List=[
    ('actinium-225', '10', '314'), 
    ('actinium-226', '1.2238', '110'), 
    ('americium-240', '2.117', '395'), 
    ('berkelium-245', '4.94', '182'),
]

The list goes on, anyways so I've been searching how to unpack these to create a dictionary so that I can calculate the radioactive decay rates of these elements given the first value is the element, the second being the half life and third the remaining stock quantity (in grams).

I need something like this

Dict={
    1: ('actinium 225', 10, 314), 
    2: ('actinium-226', 1.2238, 110), 
    3: ('americium-240', 2.117, 395),
    4: ('berkelium-245', 4.94, 182),
}
Rodrigo Rodrigues
  • 7,545
  • 1
  • 24
  • 36
Buzz Kotta
  • 21
  • 5
  • 1
    Why do you want a `dict` if your keys are just `int` numbers? Continue using a `list` instead. – Rodrigo Rodrigues Jun 06 '21 at 09:54
  • yeah... well this assignment that was dropped on us in CHEM requires us to write and test the elemental radioactive decay formula in python and i figured that dict might be the best method...BTW none of us have experience in Py – Buzz Kotta Jun 06 '21 at 10:05

5 Answers5

1

How do you turn a tuple with three elements into a dictionary containing a key and two values?

Your question title doesn't match your expected output, so I'll just go with the expected output rather than question title.

You can use map and enumerate with Dictionary Comprehension:

{idx: tuple([item[0]] + list(map(float, item[1:]))) for idx, item in enumerate(List, 1)}

OUTPUT:

{1: ('actinium-225', 10.0, 314.0),
 2: ('actinium-226', 1.2238, 110.0),
 3: ('americium-240', 2.117, 395.0),
 4: ('berkelium-245', 4.94, 182.0)}

If no type cast required, you can just do dict(enumerate(List,1))

ThePyGuy
  • 17,779
  • 5
  • 18
  • 45
  • 1
    @dontaccept , initially I wanted to post answer with dict comprehension `{i:v for i, v in enumerate(item, 1)}` (no map required). Hovewer `dict(enumerate(item,1))` is much shorter – Georgiy Jun 06 '21 at 09:55
  • I usually prefer comprehension though @Georgiy – ThePyGuy Jun 06 '21 at 09:57
  • Nice answer, but `map` is unnecesary there. You can do a [nested unpack](https://stackoverflow.com/questions/3331643/python-unpacking-an-inner-nested-tuple-list-while-still-getting-its-index-numbe), see my answer. – Rodrigo Rodrigues Jun 06 '21 at 10:21
1
dic = {}
for index, ele in enumerate(List):
    key = index+1
    val_2 = float(ele[1])
    val_3 = int(ele[2])
    dic.update({key: (ele[0], val_2, val_3)})
    
print(dic)

{1: ('actinium-225', 10.0, 314), 2: ('actinium-226', 1.2238, 110), 3: ('americium-240', 2.117, 395), 4: ('berkelium-245', 4.94, 182)}
navyad
  • 3,752
  • 7
  • 47
  • 88
1

The answer to you question is trivial, you can transform your iterable as much as you want with tools like comprehension, generated expressions, map / filter, whatever you prefer.

This one is kind of a code-golf for that:

Dict = {i: (el, float(hl), int(st)) for i, (el, hl, st) in enumerate(List, 1)}

HOWEVER,

I honestly don't see the point. If you think you need a dictionary whose keys are just ordered int numbers, you don't need a dict, you need a list!

And you already have them in a list. You can retrieve the values by index like List[2], much like (and even better) than retrieving them from a dict, like Dict[2].

Rodrigo Rodrigues
  • 7,545
  • 1
  • 24
  • 36
0

You can just loop through the tuple and create a dictionary, like this

elements = [('actinium-225', '10', '314'), ('actinium-226', '1.2238', '110'), ('americium-240', '2.117', '395'), ('berkelium-245', '4.94', '182')]
elemDict = {}
for ind, tup in enumerate(elements):
    elemDict[ind+1] = tup
aksh02
  • 178
  • 1
  • 8
0

Simple for-loop with enumerate and data destruction:

l = [
    ('actinium-225', '10', '314'), 
    ('actinium-226', '1.2238', '110'), 
    ('americium-240', '2.117', '395'), 
    ('berkelium-245', '4.94', '182'),
]
d = {}
for i, (a, b, c) in enumerate(l, 1):
    d[i] = (a, float(b), int(c))

print(d)
'''
{1: ('actinium-225', 10.0, 314), 
 2: ('actinium-226', 1.2238, 110),
 3: ('americium-240', 2.117, 395),
 4: ('berkelium-245', 4.94, 182)}
'''
XMehdi01
  • 5,538
  • 2
  • 10
  • 34