1

I have a dictionary in which I need to append new values with keys from another dictionary, though i made the for loop working, I'm asked to make a list comprehension for it. can someone help me out in this

Code:

for key, value in functionParameters.items():
    if type(value) in [int, float, bool, str]:
       if key not in self.__variables:
          self.__variables[key] = value

any help will be appreciated...

  • `__variables`? I doubt it's a good idea messing with that. And why use a list comprehension. What's the benefit? – Thomas Weller Mar 11 '22 at 15:43
  • 1
    You aren't comprehending a list so you don't need a list comprehension – Sayse Mar 11 '22 at 15:43
  • i saw this code piece : ` self.__variables = {**self.__variables, **{i: temp.get(i) for i in temp if i not in self.startlocals and i[0] != '_' and type(temp[i]) in [int, float, bool, str]}} ` and wanted to make like this –  Mar 11 '22 at 15:52
  • You're dealing with a dictionary, so how would a list comprehension be relevant? A dictionary comprehension makes the most sense, and the one you just had in your comment works well. – adang1345 Mar 11 '22 at 16:02

4 Answers4

2

Since you want to create/update a dict, you need to use dict comprehension -

self.__variables = {**self.__variables, **{k: v for k, v in functionParameters.items() if type(v) in [int, float, bool, str] and k not in self.__variables}}

Explanation -

  • z = {**x, **y} merges dicts x and y into a new dict z.
  • {k: v for k, v in functionParameters.items() if type(v) in [int, float, bool, str] and k not in self.__variables} mimics your for loop and creates a new dict
  • We are merging the original self.__variables dict with newly created dict above and saving it as self.__variables.

here's a simplifed working example -

functionParameters = {"20": 20, "string_val": "test", "float": 12.15, "pre-existing_key": "new-val", "new_type": [12, 12]}
variables = {"old_key": "val", "pre-existing_key": "val"}
variables = {**variables, **{k: v for k, v in functionParameters.items() if type(v) in [int, float, bool, str] and k not in variables}}
print(variables)

Prints -

{'old_key': 'val', 'pre-existing_key': 'val', '20': 20, 'string_val': 'test', 'float': 12.15}

Note that the value of the pre-existing_key key in output and missing new_type key since corresponding value is a list.

Jay
  • 2,431
  • 1
  • 10
  • 21
1

It's not correct to check if a key is used in an array or not in this way:

if key not in self.__variables: # not correct

if you do this it checks if the key exists as a value inside self.__variables

I don't know your reason to do this!, but you can use try & except to handle it like this:

for key, value in functionParameters.items():
    if type(value) in [int, float, bool, str]:
       try: 
          if self.__variables[key] is not None:
             self.__variables[key] = value
       except Exception ignored:
             pass
Dharman
  • 30,962
  • 25
  • 85
  • 135
Amir Shamsi
  • 319
  • 3
  • 13
  • 1
    `key not in self.__variables` for a dictionary checks if that is the *key* of the dictionary: a dictionary is an iterable of its *keys*, not its values: see https://stackoverflow.com/a/1602964/67579 – Willem Van Onsem Mar 12 '22 at 10:27
0

you are supposed to use dict comprehension for this. The code should be like this

self.__variables = {**self.__variables, **{key: value for key, value in functionParameters.items() if type(value) in [int, float, bool, str] and key not in self.__variables}}
sparsh
  • 135
  • 1
  • 1
  • 11
0

You can update with:

self.__variables.update({
    key: value
    for key, value in functionParameters.items()
    if key not in self.__variables and type(value) in [int, float, bool, str]
})
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555