Python Forum
Reference new dictionary keys with a variable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reference new dictionary keys with a variable
I want to read text files with parameters and assignments in order to configure a new device.
It seemed correct to read variables and their assignments into a dictionary from a text file.
But I have hit a snag in that I cannot work out how to read variables directly into a dictionary.
I have stripped down my script to the following to try and illustrate this.

Grateful for any replies..

# $language = "python"
# $interface = "1.0"
import time
import datetime
import sys

# This is how I want me dictionary to look after 
# assignment from reading a text file

Dict_Param = {
"<IP_ADDRESS>": "",
"<HOSTNAME>": "blahblah"

Dict_Param = {}

# Variables and their assignments are read from 
# a text file into 2 lists as shown
# I have this working just stripped out for simplicity
AssignStr = ["", "24", "blahblah"]

# This does not work
# But I hope it illustrates what I am trying 
# and failing to do
# That is add the pairs defined in the 2 lists above
# as key entries in my dictionary

Dict_Param['KeyStr[0]'] = AssignStr[0]
Dict_Param['KeyStr[1]'] = AssignStr[1]
Dict_Param['KeyStr[2]'] = AssignStr[2]

print Dict_Param
(May-06-2019, 07:17 PM)slouw Wrote:
Dict_Param['KeyStr[0]'] = AssignStr[0]
Dict_Param['KeyStr[1]'] = AssignStr[1]
Dict_Param['KeyStr[2]'] = AssignStr[2]

Remove the quotes, and that would work as you expect.

Or, you can do it the easy way and use a loop:
for index in range(len(KeyStr)):
    if index < len(AssignStr):
        key = KeyStr[index]
        val = AssignStr[index]
        Dict_Param[key] = val
values = ["", "24", "blahblah"]
dict_params = dict(zip(keys, values))
but it's interesting how you end up with two lists by reading from a text file - you can read from file line by line and directly assign value to key in the dict. I don't give example, because it's not clear how the file looks like
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

You can use zip to pair up the key and value in a for loop
dict_param = {}

assignstr = ["", "24", "blahblah"]

for key, value in zip(keystr, assignstr):
    dict_param[key] = value
{'<IP_ADDRESS>': '', '<PREFIXLENGTH>': '24', '<HOSTNAME>': 'blahblah'}
OMG what wonderful answers!
Thank you all!!! Stupendous! Amazing! Thank you all!!!

Possibly Related Threads…
Thread Author Replies Views Last Post
  How to define a variable in Python that points to or is a reference to a list member JeffDelmas 4 733 Feb-28-2021, 10:38 PM
Last Post: JeffDelmas
  Adding keys and values to a dictionary giladal 3 597 Nov-19-2020, 04:58 PM
Last Post: deanhystad
  Pass by object reference when does it behave like pass by value or reference? mczarnek 2 741 Sep-07-2020, 08:02 AM
Last Post: perfringo
  access dictionary with keys from another and write values to list redminote4dd 6 1,126 Jun-03-2020, 05:20 PM
Last Post: DeaD_EyE
  Drop Keys From Dictionary donnertrud 8 1,192 May-30-2020, 11:39 AM
Last Post: DeaD_EyE
  variable as dictionary name? diazepam 4 3,043 Apr-27-2020, 08:11 AM
Last Post: deanhystad
  Problem adding keys/values to dictionary where keynames = "property" and "value" jasonashaw 1 779 Dec-17-2019, 08:00 PM
Last Post: jasonashaw
  Checking if the combination of two keys is in a dictionary? mrsenorchuck 6 1,451 Dec-04-2019, 10:35 AM
Last Post: mrsenorchuck
  Retrieving dictionary keys within with another dictionay bazcurtis 8 1,136 Oct-29-2019, 10:06 PM
Last Post: bazcurtis
  json.dumps to keep dictionary keys batchenr 1 832 May-14-2019, 11:17 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020