Why, when changing one of the lists in the dictionary created by this function, all the other lists of this dictionary change?

def create_dict(rounds): cur_dict = {} s = requests.session() r = s.get('https://poloniex.com/public?command=returnTicker') data = r.json() roundlist = [] while len(roundlist)<rounds: roundlist.append(0) for k in sorted(data.keys()): if k.startswith('BTC'): cur_dict.update({k:roundlist}) return cur_dict 
  • 2
    All that relates to the question should be contained in the question itself, and not by reference. - Xander
  • Explain what you mean by "duplicate values ​​in the dictionary." - Xander
  • for on line 47 should transfer the value from data [key ['last'] to i column (i list item) of the cur_dict dictionary with key. A transmits all the keys. The whole 'column' eventually becomes one value. - Snez_ok
  • 2
    @Snez_ok, transfer all the necessary code directly to the question. If there is a lot of code, it is necessary to isolate the problem and write a minimal, self-sufficient and reproducible example - m9_psy

2 answers 2

In the create_dict function, you have the following code:

 roundlist = [] while len(roundlist)<rounds: roundlist.append(0) for k in sorted(data.keys()): if k.startswith('BTC'): cur_dict.update({k:roundlist}) 

That is, you create one list and assign it to each key in the dictionary.

Here it is necessary to understand that although you have written it many times in the dictionary this way, it still remains the same list.

Naturally, when you change it through one of the keys, it immediately changes across all other keys. Just because it’s actually the same list.

To make it work, you need to rewrite the function like this:

 def create_dict(rounds): cur_dict = {} s = requests.session() r = s.get('https://poloniex.com/public?command=returnTicker') data = r.json() for k in sorted(data.keys()): if k.startswith('BTC'): cur_dict.update({k: [0 for _ in range(rounds)]}) return cur_dict 

This code will create a truly separate list for each key.

PS: When you ask more questions on stackoverflow, follow the requirements for the design of questions.

PPS: By the way, I didn’t understand why you are sorting the dictionary keys before iterating over them. Does this have some kind of sacred meaning?

  • Sometimes there is not enough experience to adequately localize the problem piece. Comments on the design and sorting accepted, thank you. - Snez_ok
  • The @Snez_ok answer fixed one error, but there could be others in the code. Stylistic remarks: 1- it is unnecessary to write data.keys () (and on Python 2 it wouldn’t be effective yet), you can simply data¶ 2- instead of [0 for _ in range(rounds)] you can write [0] * rounds (numbers are immutable in Python, so there’s no problem using the same object to represent zero) ¶ 3- instead of d.update({k:expr}) it’s better to write d[k] = expr 4- this makes it obvious that sorted () is useless in code. The cycle can be written as: return {k: [0]*rounds for k in r.json() if k.startswith('BTC')} ... - jfs
  • @Snez_ok ... [continued] you can ask a question with a label inspection code , if you want to get a similar response about the working code. - jfs
  • Thank you, jfs. - Snez_ok

This is a comment published as a response to show the code.

In general, the code in question can be simplified:

 def create_dict(url, rounds): return {k: [0] * rounds for k in requests.get(url).json() if k.startswith('BTC')} 

create_dict is too common a name. Try a more specific for your task name for the created object to come up with, for example, get_foombulator(url, rounds) .