-1

I have two lists named res_list and fina_list. I want the first member of the res_list as key and first member of fina_list as the value and so on. Both the list are of the same length. I have written following code but I am not getting the answer please help me on this.

for i in range(0, len(prot_temp)):
    dict_dist[res_list_count[i]] = fina_val[i]
print (dict_dist)

res_list = ['LYS', 'LYS', 'LYS', 'LYS', 'ARG', 'LYS', 'LYS', 'LYS', 'ARG', 'LYS', 'LYS', 'ARG', 'LYS', 'ARG', 'LYS', 'LYS', 'ARG', 'LYS', 'ARG', 'ARG', 'ARG', 'LYS', 'LYS', 'LYS', 'LYS', 'LYS'] and fina_val = [['90', 30.390685694798005], ['91', 33.017900690383094], ['99', 25.200985952934463], ['104', 19.14776281971343], ['113', 20.365273138359818], ['114', 21.71637709195528], ['135', 29.452559922696025], ['138', 24.335606731700775]] but when I am doing as you said I am geeing fillowing answer: {'LYS': ['302', 25.757180610462783], 'ARG': ['282', 17.118237438474793]} where i am wrong ?

pravin kumar
  • 53
  • 1
  • 4
  • 2
    You probably want to rename your `fina_list` to `final_list`. – scharette Jul 18 '18 at 14:30
  • 3
    Possible duplicate of [Convert two lists into a dictionary in Python](https://stackoverflow.com/questions/209840/convert-two-lists-into-a-dictionary-in-python) – scharette Jul 18 '18 at 14:32

2 Answers2

4

You can use zip and a dictionary comprehension.

d = {k:v for k,v in zip(res_list, fina_list)}

Also, as Peter pointed out, one can do dict(zip(res_list, fina_list)!


Some more explicit (and perhaps less Python(ic) ways)

You can iterate over the zip to be more explicit.

d = {}
for k,v in zip(res_list, fina_list):
    d[k] = v

Of you can even iterate over the lists together with an index.

d = {}
for i in range(min(len(res_list), len(fina_list)):
    d[res_list[i]] = fina_list[i]

if those are more clear to you. However, part of the beauty of python is it's conciseness while maintaining clarity.

modesitt
  • 7,052
  • 2
  • 34
  • 64
  • 5
    Or just use the [**`dict`**](https://docs.python.org/3/library/stdtypes.html#dict) constructor, which can take tuple pairs: `dict(zip(keys, values))` – Peter Wood Jul 18 '18 at 14:31
  • @PeterWood That would be an exact duplicate though. See comment section. – scharette Jul 18 '18 at 14:32
  • 2
    @scharette a comprehensive answer can mention more than one method. Also, the answer wasn't there when I commented. – Peter Wood Jul 18 '18 at 14:34
  • @PeterWood No, you missunderstood me. Your insight was really good. It was actually the best to acheive this. My point was that there is an exact duplicate that I inlcuded in the comments section. – scharette Jul 18 '18 at 14:36
2

dict's constructor accepts a sequence of (key, value) pairs, so you can achieve it with

dict(zip(res_list, fina_list))
taras
  • 6,566
  • 10
  • 39
  • 50
  • res_list = ['LYS', 'LYS', 'LYS', 'LYS', 'ARG', 'LYS', 'LYS', 'LYS', 'ARG', 'LYS', 'LYS', 'ARG', 'LYS', 'ARG', 'LYS', 'LYS', 'ARG', 'LYS', 'ARG', 'ARG', 'ARG', 'LYS', 'LYS', 'LYS', 'LYS', 'LYS'] and fina_val = [['90', 30.390685694798005], ['91', 33.017900690383094], ['99', 25.200985952934463], ['104', 19.14776281971343], ['113', 20.365273138359818], ['114', 21.71637709195528], ['135', 29.452559922696025], ['138', 24.335606731700775]] but when I am doing as you said I am geeing fillowing answer: {'LYS': ['302', 25.757180610462783], 'ARG': ['282', 17.118237438474793]} where i am wrong ? – pravin kumar Jul 20 '18 at 10:00
  • Since you have duplicated in res_list you need to decide on a proper way to handle it. A dictionary cannot have duplicate keys (for obvious reasons) – taras Jul 20 '18 at 10:04
  • OOPS than what I could do ? – pravin kumar Jul 20 '18 at 10:08
  • You can either aggregate values with the same keys in a list or rename your keys to avoid duplicates. – taras Jul 20 '18 at 10:10
  • Please, update your question with those sample `res_list` and `fina_val`, since it is a major point – taras Jul 20 '18 at 10:16