473,386 Members | 1,793 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Dictionary problem when key value is taken from a variable

Hi,
I have a dictionary whose keys are variables.
Eg: - dict=None
dict[lst]=value
Here lst and values are 2 variables. How can I make the key
as the value of lst.
For eg:- if lst="node1" and if value = 10,
this is what i needs..

dict["node1"]=10
Thanx..
Apr 18 '08 #1
2 24902
bvdet
2,851 Expert Mod 2GB
Hi,
I have a dictionary whose keys are variables.
Eg: - dict=None
dict[lst]=value
Here lst and values are 2 variables. How can I make the key
as the value of lst.
For eg:- if lst="node1" and if value = 10,
this is what i needs..

dict["node1"]=10
Thanx..
Don't use the Python built-in function dict for a variable name. Set your dd to an empty dictionary, not None.
Expand|Select|Wrap|Line Numbers
  1. >>> dd = {}
  2. >>> lst = 'Node1'
  3. >>> value = 10
  4. >>> dd[lst]=value
  5. >>> dd
  6. {'Node1': 10}
  7. >>> 
Apr 18 '08 #2
jlm699
314 100+
In case you wanted to use some values during the initialization of a dictionary you could do:
Expand|Select|Wrap|Line Numbers
  1. >>> lst = 'Node1'
  2. >>> value = 10
  3. >>> dd = { lst:value }
  4. >>> dd
  5. {'Node1': 10}
  6. >>> 
Apr 23 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Thomas Philips | last post by:
I want to print "1 spam 4 you" using a formatted string that gets its inputs from the dictionary d={'n1':1, 's1':'spam', 'n2':4}. To do so, I write >>> x="%('n1')d %('s1')s %('n2')d you" >>> x...
8
by: Jack Carter | last post by:
I have been delegated to produce a tool that has python embedded in it. The desire is to have a command line interface that inherits all the python scripting functionality so people can use the...
125
by: Raymond Hettinger | last post by:
I would like to get everyone's thoughts on two new dictionary methods: def count(self, value, qty=1): try: self += qty except KeyError: self = qty def appendlist(self, key, *values): try:
1
by: boohoo | last post by:
I can't seem to do this: I want to take a query string and place two halves of the querystring into two separate dictionary objects. So... I loop through the collection of querystring items,...
20
by: weston | last post by:
I've got a piece of code where, for all the world, it looks like this fails in IE 6: hometab = document.getElementById('hometab'); but this succeeds: hometabemt =...
3
by: JamesB | last post by:
I have a config screen in my app that updates a dictionary<key,value>. I want to store a copy of this before going into the config screen so if the user wants to cancel all their changes I can...
3
by: Mitko Haralanov | last post by:
I have a Python module that I have written using the C API and I am having a problem accessing a dictionary from that module. Here is what I have done: 1. In my init function I call module =...
2
by: Andy B | last post by:
I don't know if this is even working or not but here is the problem. I have a gridview that I databound to a dictionary<string, stringcollection: Contract StockContract = new Contract();...
8
by: Bob Altman | last post by:
Hi all, I'm trying to do something that should be really easy, but I can't think of an obvious way to do it. I have a dictionary whose value is a "value type" (as opposed to a reference type --...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.