Value of a key in a Python dictinary is not updating [duplicate]

2024/11/16 1:22:19

I have a weird problem, as I am trying to generate a list of dictionaries to pass them as a parameter to a function. Designing the input "by hand" looks like this:

params = [# Amazon{'q': "AMZN",'x': "NASDAQ",},{'q': "PIH",'x': "NASDAQ",},{'q': "AIR",'x': "NYSE",},{'q': "FCO",'x': "NYSEAMERICAN",},{'q': "7201",'x': "TYO",}
].

I have tried to generate a similar list of dictionaries from a txt file containing a list of tickers (one per line) with the following code:

info = {}
params = []
with open('Test_Tickers.txt') as f:for line in f:info['q'] = line.rstrip()info['x'] = "NYSE"params.append(info)print(info)

The frustrating part is that while the print(info) returns the correct dictionaries

{'q': 'ABB', 'x': 'NYSE'}
{'q': 'ABBV', 'x': 'NYSE'}
{'q': 'ABC', 'x': 'NYSE'}
{'q': 'ABEV', 'x': 'NYSE'}
...
{'q': 'IJS', 'x': 'NYSE'}

the params looks like this:

[{'q': 'IJS', 'x': 'NYSE'}, {'q': 'IJS', 'x': 'NYSE'}, {'q': 'IJS', 'x':         'NYSE'}, {'q': 'IJS', 'x': 'NYSE'}, {'q': 'IJS', 'x': 'NYSE'}, {'q': 'IJS', 'x': 'NYSE'}, {'q': 'IJS', 'x': 'NYSE'}, {'q': 'IJS', 'x': 'NYSE'}, ... ]

How can I corrent the code so that the dictionaries contain all the tickers and not only the last one?

Answer
params = []
with open('Test_Tickers.txt') as f:for line in f:info = {}info['q'] = line.rstrip()info['x'] = "NYSE"params.append(info)print(info)

I will work

you were updating only one dict object in list , to make multiple object you have to define info = {} inside the loop

https://en.xdnf.cn/q/119248.html

Related Q&A

Python - __init__() missing 1 required positional argument:

Im kinda new to python and I cant get past this error: Traceback (most recent call last):File "***", line 63, in <module>bst = Node() TypeError: __init__() missing 1 required positional…

discord py - Custom command prefix doesnt work (no command run)

i have a problem that i cant solve. Im trying to add a prefix switcher for all guilds, that uses my bot. So Ive done that, but currently no command gets triggered and I cant find a solution since hours…

How to use sep parameter in .format?

I just started learning python and Im experimenting new things. isim = input("Name:") soyad = input("Surname:") yaş = input("Age:") edu = input("Education:") ge…

Python table classification

I have different type of data for example:4.5,3.5,U1 4.5,10.5,U2 4.5,6,U1 3.5,10.5,U2 3.5,10.5,U2 5,7,U1 7,6.5,U1I need output:U1: [[4.5, 3.5], [4.5, 6], [5, 7], [7, 6.5]] U2: [[4.5, 10.5], [3.5, 10.5]…

python json.loads / json.load truncates nested json objects?

given the following code: import json foo = {"root":"cfb-score","children":{"gamecode":{"attribute":"global-id"},"gamestate":{&quo…

How to make an encrypted executable file

I have made a tool/program on Ubuntu written in Python. I want to give this to my friend to test on his PC, but I dont want to share the source code.This program has many folders and many .py files. Is…

Organizing pythonic dictionaries for a JSON schema validation

Scenario: I am trying to create a JSON schema validator in python. In this case, I am building a dictionary which contain the information that will be used for the validation.Code:import json import os…

Scraping a specific website with a search box and javascripts in Python

On the website https://sray.arabesque.com/dashboard there is a search box "input" in html. I want to enter a company name in the search box, choose the first suggestion for that name in the d…

Uppercasing letters after ., ! and ? signs in Python

I have been searching Stack Overflow but cannot find the proper code for correcting e.g."hello! are you tired? no, not at all!"Into:"Hello! Are you tired? No, not at all!"

Why does list() function is not letting me change the list [duplicate]

This question already has answers here:How do I clone a list so that it doesnt change unexpectedly after assignment?(24 answers)Python pass by value with nested lists?(1 answer)Closed 2 years ago.If …