1

I have a list of tuples looking like this:

[(5, 0, 1, 8), (5, 0, 1, 14, 15), (5, 0, 1, 14, 16)]

I also have a dictionary that is in the form:

{0: [], 1: [], 2: [], 3: [], 4: [], 5: ['CVE-2016-3379'], 7: ['CVE-2016-3646'], 8: [], 9: ['CVE-2015-1769'], 10: ['CVE-2016-3364', 'CVE-2016-7193', 'CVE-2016-3363'], 14: [], 15: ['CVE-2015-1769'], 16: ['CVE-2016-3363', 'CVE-2016-7193', 'CVE-2016-3364'], 17: [], 18: [], 19: [], 20: [], 21: ['CVE-2015-1769'], 22: ['CVE-2016-3363', 'CVE-2016-7193', 'CVE-2016-3364'], 26: [], 27: [], 28: [], 29: [], 30: [], 32: []}

How can I update the values in the first list with the values from the dictionary?

I want 5 for example to become 5: ['CVE-2016-3379'], 0 to become 0: [] etc.

3
  • Show us what you've already tried. SO isn't a code-writing service. Commented Nov 2, 2017 at 15:59
  • Please put that info in your question, where it belongs. And it's pointless putting multi-line Python code into comments since the indentation gets lost. Commented Nov 2, 2017 at 16:03
  • Thank you for the answer. This is not a coding assisgnment, it is a part of a project that is confidential and my python skills are not that great. Commented Nov 2, 2017 at 16:12

2 Answers 2

3

You can do that with this list comprehension:

res = [tuple('{}: {}'.format(i, my_dict[i]) for i in t) for t in my_list]

Example of output:

>>> my_list = [(5, 0, 1, 8)]
>>> my_dict = {0: [], 1: [], 5: ['CVE-2016-3379'], 8: []}
>>> res = [tuple('{}: {}'.format(i, my_dict[i]) for i in t) for t in my_list]
>>> res
[("5: ['CVE-2016-3379']", '0: []', '1: []', '8: []')]
Sign up to request clarification or add additional context in comments.

1 Comment

Quite possible that this is what the OP means by his desired output :)
1

Given your list is called lst and the dict is called dct, you can use the following nested comprehension:

lst = [tuple(dct[x] for x in tpl) for tpl in lst]

Since the tuples in your list are immutable, you can not just update their cells. You have to rebuild them with the new values.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.