1

I have a list that contains 5 variables, say:

list2=[1,2,3,4,5] 

and I have a list of dict with 5 key-value pairs, which I initialized to be :

list1[i]= {"input1": 0, "input2": 0, "input3": 0, "input4": 0, "input5": 0}

I want to iterate over the dict and list so that in each iteration, I will replace the value of a key in the dict with the value from the list, so the dict will become:

list1[i]= {"input1": 1, "input2": 2, "input3": 3, "input4": 4, "input5": 5}

Currently, I use this to iterate over the dict and list:

def Get_Param(self):
    self.list1=[]
    inputcount=0
    for line in self.textBox.get('1.0', 'end-1c').splitlines():
        if line:
            self.list1.append({'input1':0, 'input2':0, 'input3': 0, 'input4': 0, 'input5': 0})
            list2=[int(i) for i in line.split()]
            for parameter, value in zip(self.list1[inputcount], list2): //exception is thrown here
                self.list1[inputcount][parameter]=value
        inputcount+=1

but it keeps returning "list indices must be integers, not Unicode" exception. Can anyone suggest a better idea to do this or tell me in which part of the code I did wrong?

Reza
  • 25
  • 1
  • 8
  • 5
    You should avoid using defined values such as `dict` for variable names - what is `dict` initialized to in your case? Note `dict[i]` is not clear as to what `dict` actually is. – metatoaster Oct 11 '17 at 04:52
  • originally I declared dict=[ ], anyway dict here is just for example purpose, I used other variable name in my code – Reza Oct 11 '17 at 04:54
  • 2
    note that `dict` are not ordered, so you might have unexpected results... – Julien Oct 11 '17 at 04:54
  • What is the value of `i`? – metatoaster Oct 11 '17 at 04:54
  • i is of course an integer, I didn't include the rest of the code to keep the question short – Reza Oct 11 '17 at 04:56
  • @Julien As of Python 3.6+ dicts *are* ordered https://stackoverflow.com/q/39980323/6622817 but it doesn’t matter here as this question is for python 2.7 – Taku Oct 11 '17 at 04:56
  • Seems to work for me. Often, the errors happen because in the actual code, the values aren't as expected. See if you can share your actual code snippet that throws an error. – ShreyasG Oct 11 '17 at 04:57
  • @Reza you need to post a Minimal, Complete, Verifiable Example. Or else, there’s no way you can get a well enough answer, as we couldn’t even reproduce your problem. You’ll need to create a code so that it could reproduce the error that you mentioned just by running your code block, **and no additional code or inputs are required**. This might mean that you’ll need to modify your code a bit. – Taku Oct 11 '17 at 04:59
  • Alright, I've put my full code in the question – Reza Oct 11 '17 at 05:01
  • @PaulRooney dict is a list , but dict [i] is a dictionary – Reza Oct 11 '17 at 05:09
  • Do keys in dictionary have any pattern that can be related to the indices of list? – gautamaggarwal Oct 11 '17 at 05:13

3 Answers3

2

You can reduce a lot of the extraneous code by simply creating two lists (keys and associated values), and using a dictionary comprehension to join them into your final dictionary. This solves the issue of trying to map an ordered list onto an unordered dictionary.

def Get_Param(self):
    self.list1 = []
    dict_keys = ['input1', 'input2', 'input3', 'input4', 'input5']
    for line in self.textBox.get('1.0', 'end-1c').splitlines():
        if line:
            list2 = [int(i) for i in line.split()]
            self.list1.append({key: value for key, value in zip(dict_keys, list2)})
Reza
  • 25
  • 1
  • 8
Andrew Guy
  • 9,310
  • 3
  • 28
  • 40
0

You can zip your dicts sorted keys with the list and produce a new dict. That matches the numbers in the list and the dict.

>>> list2=[1,2,3,4,5]
>>> list1=[{"input1": 0, "input2": 0, "input3": 0, "input4": 0, "input5": 0}]
>>> dict(zip(sorted(list1[0].keys()), list2))
{'input4': 4, 'input1': 1, 'input5': 5, 'input2': 2, 'input3': 3}

The dict itself is not ordered but the indices match. You dont actually even need the call to keys(). It will still work without it.

This shows how to build a single dict. To create a list of them simply do this in a loop.

Reza
  • 25
  • 1
  • 8
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
-1

You can refer this solution and customize to solve your problem:

list1=[1,2,3,4,5] 
dict1= {"input1": 0, "input2": 0, "input3": 0, "input4": 0, "input5": 0}

d = {}
for i in dict1:
    d[i] = list1[int(i.replace("input",''))-1]
print d

Output : {'input2': 2, 'input3': 3, 'input1': 1, 'input4': 4, 'input5': 5}

Pradip Das
  • 728
  • 1
  • 7
  • 16
  • Doesn't solve the issue that dictionaries are not ordered, and OP is trying to map an ordered list to an unordered dictionary. – Andrew Guy Oct 11 '17 at 05:10
  • if you are referring the index of list by the dict key number i.e input then this solution will work – Pradip Das Oct 12 '17 at 04:12