Python getting a string (key + value) from Python Dictionary
Question:
I have dictionary structure. For example:
dict = {key1 : value1 ,key2 : value2}
What I want is the string which combines the key and the value
Needed string –>> key1_value1 , key2_value2
Any Pythonic way to get this will help.
Thanks
def checkCommonNodes( id , rs):
for r in rs:
for key , value in r.iteritems():
kv = key+"_"+value
if kv == id:
print "".join('{}_{}'.format(k,v) for k,v in r.iteritems())
Answers:
A list
of key-value str
s,
>>> d = {'key1': 'value1', 'key2': 'value2'}
>>> ['{}_{}'.format(k,v) for k,v in d.iteritems()]
['key2_value2', 'key1_value1']
Or if you want a single string of all key-value pairs,
>>> ', '.join(['{}_{}'.format(k,v) for k,v in d.iteritems()])
'key2_value2, key1_value1'
EDIT:
Maybe you are looking for something like this,
def checkCommonNodes(id, rs):
id_key, id_value = id.split('_')
for r in rs:
try:
if r[id_key] == id_value:
print "".join('{}_{}'.format(k,v) for k,v in r.iteritems())
except KeyError:
continue
You may also be wanting to break
after print
ing – hard to know exactly what this is for.
Assuming Python 2.x, I would use something like this
dict = {'key1': 'value1', 'key2': 'value2'}
str = ''.join(['%s_%s' % (k,v) for k,v in dict.iteritems()])
def checkCommonNodes(id, rs):
k,v = id.split('_')
for d in rs:
if d.get(k) == v:
return id
retun None
Updated answer for Python 3.x
Example one – join a single key, value in the form "key_value"
k = 'key1'
v = 'value1'
mystring = f'{k}={v}'
print(mystring)
# result -> key1=value1
Example two: create a list of all key-value pairs as strings in the form "key_value"
mydict = {'key1': 'value1', 'key2': 'value2'}
mylist = [f'{k}_{v}' for k,v in mydict.items()]
print(mylist)
# result -> ['key1_value1', 'key2_value2']
Example three: transform a list to a string
result = ', '.join(mylist)
print(result)
# result -> key1_value1, key2_value2
Putting it all together – join all key-value pairs in a dictionary and output as a string.
mydict = {'key1': 'value1', 'key2': 'value2'}
result = ', '.join([f'{k}_{v}' for k,v in mydict.items()])
print(result)
# result -> key1_value1, key2_value2
This is the same basic answer provided by Jared, but now with Python 3.x we use the .items()
function instead of .iteritems()
, and we can use an f-string instead of the string format()
function (although the latter does still work too).
This may not be exactly the perfect answer to the original question asked 12+ years ago (!) but it is a more generic answer about transforming dictionary key-value pairs to (key + value), as indicated by the question title.
I have dictionary structure. For example:
dict = {key1 : value1 ,key2 : value2}
What I want is the string which combines the key and the value
Needed string –>> key1_value1 , key2_value2
Any Pythonic way to get this will help.
Thanks
def checkCommonNodes( id , rs):
for r in rs:
for key , value in r.iteritems():
kv = key+"_"+value
if kv == id:
print "".join('{}_{}'.format(k,v) for k,v in r.iteritems())
A list
of key-value str
s,
>>> d = {'key1': 'value1', 'key2': 'value2'}
>>> ['{}_{}'.format(k,v) for k,v in d.iteritems()]
['key2_value2', 'key1_value1']
Or if you want a single string of all key-value pairs,
>>> ', '.join(['{}_{}'.format(k,v) for k,v in d.iteritems()])
'key2_value2, key1_value1'
EDIT:
Maybe you are looking for something like this,
def checkCommonNodes(id, rs):
id_key, id_value = id.split('_')
for r in rs:
try:
if r[id_key] == id_value:
print "".join('{}_{}'.format(k,v) for k,v in r.iteritems())
except KeyError:
continue
You may also be wanting to break
after print
ing – hard to know exactly what this is for.
Assuming Python 2.x, I would use something like this
dict = {'key1': 'value1', 'key2': 'value2'}
str = ''.join(['%s_%s' % (k,v) for k,v in dict.iteritems()])
def checkCommonNodes(id, rs):
k,v = id.split('_')
for d in rs:
if d.get(k) == v:
return id
retun None
Updated answer for Python 3.x
Example one – join a single key, value in the form "key_value"
k = 'key1'
v = 'value1'
mystring = f'{k}={v}'
print(mystring)
# result -> key1=value1
Example two: create a list of all key-value pairs as strings in the form "key_value"
mydict = {'key1': 'value1', 'key2': 'value2'}
mylist = [f'{k}_{v}' for k,v in mydict.items()]
print(mylist)
# result -> ['key1_value1', 'key2_value2']
Example three: transform a list to a string
result = ', '.join(mylist)
print(result)
# result -> key1_value1, key2_value2
Putting it all together – join all key-value pairs in a dictionary and output as a string.
mydict = {'key1': 'value1', 'key2': 'value2'}
result = ', '.join([f'{k}_{v}' for k,v in mydict.items()])
print(result)
# result -> key1_value1, key2_value2
This is the same basic answer provided by Jared, but now with Python 3.x we use the .items()
function instead of .iteritems()
, and we can use an f-string instead of the string format()
function (although the latter does still work too).
This may not be exactly the perfect answer to the original question asked 12+ years ago (!) but it is a more generic answer about transforming dictionary key-value pairs to (key + value), as indicated by the question title.