Print dict iteration elements as it is read(declared) [duplicate]
This question already has an answer here:
Dictionaries: How to keep keys/values in same order as declared?
11 answers
I am reading a dictionary in python2.6 as below
I know Python3.6 will read the dictionary in the same order it is declared, but i need to achieve this in Python2.6 (OrderedDict is also not available in Python2.6)
numbermap = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
>>> for k, v in numbermap.iteritems():
... print(k,v)
...
('four', 4)
('three', 3)
('five', 5)
('two', 2)
('one', 1)
I want the output to be
('one',1)
('two', 2)
('three', 3)
('four', 4)
('five', 5)
I need to write as I read the dictionary. Any ideas to achieve this in Python 2.6?
python dictionary python-2.6 iteritems
marked as duplicate by jonrsharpe, Patrick Artner, jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 14:25
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Dictionaries: How to keep keys/values in same order as declared?
11 answers
I am reading a dictionary in python2.6 as below
I know Python3.6 will read the dictionary in the same order it is declared, but i need to achieve this in Python2.6 (OrderedDict is also not available in Python2.6)
numbermap = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
>>> for k, v in numbermap.iteritems():
... print(k,v)
...
('four', 4)
('three', 3)
('five', 5)
('two', 2)
('one', 1)
I want the output to be
('one',1)
('two', 2)
('three', 3)
('four', 4)
('five', 5)
I need to write as I read the dictionary. Any ideas to achieve this in Python 2.6?
python dictionary python-2.6 iteritems
marked as duplicate by jonrsharpe, Patrick Artner, jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 14:25
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
Python dictionaries aren't ordered data structures.
– jonrsharpe
Nov 15 '18 at 14:18
Any way to sorted as i read them?
– Venkat J
Nov 15 '18 at 14:19
1
Use an OrderedDict from the collections module if applicable in 2.6 - do you know about pythonclock.org ? Time to update. If you switch to 3.6 CPython or AnyPythong 3.7 you get insert order guranteed for nothing
– Patrick Artner
Nov 15 '18 at 14:22
1
Just saw: OrderedDict is not available for 2.6 - if you want them as you printed them, the only way to achieve it is to sort by value - but thats less then ideal because it does not reflect your insert order at all ... just put a"ninetynine":99
in the first place ... its only by the values choosen that this fits.
– Patrick Artner
Nov 15 '18 at 14:28
unfortunately, i have to do it in Python2.6.6, Is there anyway because OrderedDict is not available.
– Venkat J
Nov 15 '18 at 14:44
add a comment |
This question already has an answer here:
Dictionaries: How to keep keys/values in same order as declared?
11 answers
I am reading a dictionary in python2.6 as below
I know Python3.6 will read the dictionary in the same order it is declared, but i need to achieve this in Python2.6 (OrderedDict is also not available in Python2.6)
numbermap = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
>>> for k, v in numbermap.iteritems():
... print(k,v)
...
('four', 4)
('three', 3)
('five', 5)
('two', 2)
('one', 1)
I want the output to be
('one',1)
('two', 2)
('three', 3)
('four', 4)
('five', 5)
I need to write as I read the dictionary. Any ideas to achieve this in Python 2.6?
python dictionary python-2.6 iteritems
This question already has an answer here:
Dictionaries: How to keep keys/values in same order as declared?
11 answers
I am reading a dictionary in python2.6 as below
I know Python3.6 will read the dictionary in the same order it is declared, but i need to achieve this in Python2.6 (OrderedDict is also not available in Python2.6)
numbermap = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
>>> for k, v in numbermap.iteritems():
... print(k,v)
...
('four', 4)
('three', 3)
('five', 5)
('two', 2)
('one', 1)
I want the output to be
('one',1)
('two', 2)
('three', 3)
('four', 4)
('five', 5)
I need to write as I read the dictionary. Any ideas to achieve this in Python 2.6?
This question already has an answer here:
Dictionaries: How to keep keys/values in same order as declared?
11 answers
python dictionary python-2.6 iteritems
python dictionary python-2.6 iteritems
edited Nov 15 '18 at 15:26
Venkat J
asked Nov 15 '18 at 14:18
Venkat JVenkat J
266
266
marked as duplicate by jonrsharpe, Patrick Artner, jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 14:25
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by jonrsharpe, Patrick Artner, jpp
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 15 '18 at 14:25
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
2
Python dictionaries aren't ordered data structures.
– jonrsharpe
Nov 15 '18 at 14:18
Any way to sorted as i read them?
– Venkat J
Nov 15 '18 at 14:19
1
Use an OrderedDict from the collections module if applicable in 2.6 - do you know about pythonclock.org ? Time to update. If you switch to 3.6 CPython or AnyPythong 3.7 you get insert order guranteed for nothing
– Patrick Artner
Nov 15 '18 at 14:22
1
Just saw: OrderedDict is not available for 2.6 - if you want them as you printed them, the only way to achieve it is to sort by value - but thats less then ideal because it does not reflect your insert order at all ... just put a"ninetynine":99
in the first place ... its only by the values choosen that this fits.
– Patrick Artner
Nov 15 '18 at 14:28
unfortunately, i have to do it in Python2.6.6, Is there anyway because OrderedDict is not available.
– Venkat J
Nov 15 '18 at 14:44
add a comment |
2
Python dictionaries aren't ordered data structures.
– jonrsharpe
Nov 15 '18 at 14:18
Any way to sorted as i read them?
– Venkat J
Nov 15 '18 at 14:19
1
Use an OrderedDict from the collections module if applicable in 2.6 - do you know about pythonclock.org ? Time to update. If you switch to 3.6 CPython or AnyPythong 3.7 you get insert order guranteed for nothing
– Patrick Artner
Nov 15 '18 at 14:22
1
Just saw: OrderedDict is not available for 2.6 - if you want them as you printed them, the only way to achieve it is to sort by value - but thats less then ideal because it does not reflect your insert order at all ... just put a"ninetynine":99
in the first place ... its only by the values choosen that this fits.
– Patrick Artner
Nov 15 '18 at 14:28
unfortunately, i have to do it in Python2.6.6, Is there anyway because OrderedDict is not available.
– Venkat J
Nov 15 '18 at 14:44
2
2
Python dictionaries aren't ordered data structures.
– jonrsharpe
Nov 15 '18 at 14:18
Python dictionaries aren't ordered data structures.
– jonrsharpe
Nov 15 '18 at 14:18
Any way to sorted as i read them?
– Venkat J
Nov 15 '18 at 14:19
Any way to sorted as i read them?
– Venkat J
Nov 15 '18 at 14:19
1
1
Use an OrderedDict from the collections module if applicable in 2.6 - do you know about pythonclock.org ? Time to update. If you switch to 3.6 CPython or AnyPythong 3.7 you get insert order guranteed for nothing
– Patrick Artner
Nov 15 '18 at 14:22
Use an OrderedDict from the collections module if applicable in 2.6 - do you know about pythonclock.org ? Time to update. If you switch to 3.6 CPython or AnyPythong 3.7 you get insert order guranteed for nothing
– Patrick Artner
Nov 15 '18 at 14:22
1
1
Just saw: OrderedDict is not available for 2.6 - if you want them as you printed them, the only way to achieve it is to sort by value - but thats less then ideal because it does not reflect your insert order at all ... just put a
"ninetynine":99
in the first place ... its only by the values choosen that this fits.– Patrick Artner
Nov 15 '18 at 14:28
Just saw: OrderedDict is not available for 2.6 - if you want them as you printed them, the only way to achieve it is to sort by value - but thats less then ideal because it does not reflect your insert order at all ... just put a
"ninetynine":99
in the first place ... its only by the values choosen that this fits.– Patrick Artner
Nov 15 '18 at 14:28
unfortunately, i have to do it in Python2.6.6, Is there anyway because OrderedDict is not available.
– Venkat J
Nov 15 '18 at 14:44
unfortunately, i have to do it in Python2.6.6, Is there anyway because OrderedDict is not available.
– Venkat J
Nov 15 '18 at 14:44
add a comment |
3 Answers
3
active
oldest
votes
There are many practices available for the sorting dictionary. You can check below examples.
First example:
>>> import operator
>>> numbermap = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
>>> sorted_maps = sorted(numbermap.items(), key=operator.itemgetter(1))
>>> print(sorted_maps)
[('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5)]
Second example:
>>> import collections
>>> sorted_maps = collections.OrderedDict(numbermap)
>>> print(sorted_maps)
OrderedDict([('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5)])
2.7 for OrderedDict - not in 2.6 - and your first approach only works for this set of values - not for f.e.{'nine':9, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
– Patrick Artner
Nov 15 '18 at 14:29
add a comment |
It seems that you want an ordered dictionary. If you can use Python 2.7, look up collections.OrderedDict
: https://docs.python.org/2/library/collections.html#collections.OrderedDict
If you have to stick with 2.6, there are some suggestions here: https://stackoverflow.com/a/1617087/3061818 (but you should probably head over to Dictionaries: How to keep keys/values in same order as declared?)
3
The docs sayNew in version 2.7
and the question is tagged withpython-2.6
.
– ikkuh
Nov 15 '18 at 14:24
Oooh, I didn't see that. Thanks.
– ukrutt
Nov 16 '18 at 15:40
add a comment |
1 reverse the key value,
2 sort the new key which is the value
my solution is to sort the keys
sounds like cheating, but works:
first call something to reverse dict
for i in sort(numbermap.keys()):
print(i,numbermap[i])
This won't work, because the order of keys is not the order the OP wants.
– jonrsharpe
Nov 15 '18 at 14:26
if you sort the key alphabetically - it wont.four
beforeone
just to name one
– Patrick Artner
Nov 15 '18 at 14:26
just an idea, u can reverse the key value and order the new key which is the current value
– 陈海栋
Nov 15 '18 at 14:34
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
There are many practices available for the sorting dictionary. You can check below examples.
First example:
>>> import operator
>>> numbermap = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
>>> sorted_maps = sorted(numbermap.items(), key=operator.itemgetter(1))
>>> print(sorted_maps)
[('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5)]
Second example:
>>> import collections
>>> sorted_maps = collections.OrderedDict(numbermap)
>>> print(sorted_maps)
OrderedDict([('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5)])
2.7 for OrderedDict - not in 2.6 - and your first approach only works for this set of values - not for f.e.{'nine':9, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
– Patrick Artner
Nov 15 '18 at 14:29
add a comment |
There are many practices available for the sorting dictionary. You can check below examples.
First example:
>>> import operator
>>> numbermap = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
>>> sorted_maps = sorted(numbermap.items(), key=operator.itemgetter(1))
>>> print(sorted_maps)
[('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5)]
Second example:
>>> import collections
>>> sorted_maps = collections.OrderedDict(numbermap)
>>> print(sorted_maps)
OrderedDict([('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5)])
2.7 for OrderedDict - not in 2.6 - and your first approach only works for this set of values - not for f.e.{'nine':9, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
– Patrick Artner
Nov 15 '18 at 14:29
add a comment |
There are many practices available for the sorting dictionary. You can check below examples.
First example:
>>> import operator
>>> numbermap = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
>>> sorted_maps = sorted(numbermap.items(), key=operator.itemgetter(1))
>>> print(sorted_maps)
[('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5)]
Second example:
>>> import collections
>>> sorted_maps = collections.OrderedDict(numbermap)
>>> print(sorted_maps)
OrderedDict([('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5)])
There are many practices available for the sorting dictionary. You can check below examples.
First example:
>>> import operator
>>> numbermap = {'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
>>> sorted_maps = sorted(numbermap.items(), key=operator.itemgetter(1))
>>> print(sorted_maps)
[('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5)]
Second example:
>>> import collections
>>> sorted_maps = collections.OrderedDict(numbermap)
>>> print(sorted_maps)
OrderedDict([('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5)])
edited Nov 15 '18 at 14:37
answered Nov 15 '18 at 14:24
Adem ÖztaşAdem Öztaş
12.3k22534
12.3k22534
2.7 for OrderedDict - not in 2.6 - and your first approach only works for this set of values - not for f.e.{'nine':9, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
– Patrick Artner
Nov 15 '18 at 14:29
add a comment |
2.7 for OrderedDict - not in 2.6 - and your first approach only works for this set of values - not for f.e.{'nine':9, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
– Patrick Artner
Nov 15 '18 at 14:29
2.7 for OrderedDict - not in 2.6 - and your first approach only works for this set of values - not for f.e.
{'nine':9, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
– Patrick Artner
Nov 15 '18 at 14:29
2.7 for OrderedDict - not in 2.6 - and your first approach only works for this set of values - not for f.e.
{'nine':9, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
– Patrick Artner
Nov 15 '18 at 14:29
add a comment |
It seems that you want an ordered dictionary. If you can use Python 2.7, look up collections.OrderedDict
: https://docs.python.org/2/library/collections.html#collections.OrderedDict
If you have to stick with 2.6, there are some suggestions here: https://stackoverflow.com/a/1617087/3061818 (but you should probably head over to Dictionaries: How to keep keys/values in same order as declared?)
3
The docs sayNew in version 2.7
and the question is tagged withpython-2.6
.
– ikkuh
Nov 15 '18 at 14:24
Oooh, I didn't see that. Thanks.
– ukrutt
Nov 16 '18 at 15:40
add a comment |
It seems that you want an ordered dictionary. If you can use Python 2.7, look up collections.OrderedDict
: https://docs.python.org/2/library/collections.html#collections.OrderedDict
If you have to stick with 2.6, there are some suggestions here: https://stackoverflow.com/a/1617087/3061818 (but you should probably head over to Dictionaries: How to keep keys/values in same order as declared?)
3
The docs sayNew in version 2.7
and the question is tagged withpython-2.6
.
– ikkuh
Nov 15 '18 at 14:24
Oooh, I didn't see that. Thanks.
– ukrutt
Nov 16 '18 at 15:40
add a comment |
It seems that you want an ordered dictionary. If you can use Python 2.7, look up collections.OrderedDict
: https://docs.python.org/2/library/collections.html#collections.OrderedDict
If you have to stick with 2.6, there are some suggestions here: https://stackoverflow.com/a/1617087/3061818 (but you should probably head over to Dictionaries: How to keep keys/values in same order as declared?)
It seems that you want an ordered dictionary. If you can use Python 2.7, look up collections.OrderedDict
: https://docs.python.org/2/library/collections.html#collections.OrderedDict
If you have to stick with 2.6, there are some suggestions here: https://stackoverflow.com/a/1617087/3061818 (but you should probably head over to Dictionaries: How to keep keys/values in same order as declared?)
edited Nov 16 '18 at 15:46
answered Nov 15 '18 at 14:23
ukruttukrutt
83021120
83021120
3
The docs sayNew in version 2.7
and the question is tagged withpython-2.6
.
– ikkuh
Nov 15 '18 at 14:24
Oooh, I didn't see that. Thanks.
– ukrutt
Nov 16 '18 at 15:40
add a comment |
3
The docs sayNew in version 2.7
and the question is tagged withpython-2.6
.
– ikkuh
Nov 15 '18 at 14:24
Oooh, I didn't see that. Thanks.
– ukrutt
Nov 16 '18 at 15:40
3
3
The docs say
New in version 2.7
and the question is tagged with python-2.6
.– ikkuh
Nov 15 '18 at 14:24
The docs say
New in version 2.7
and the question is tagged with python-2.6
.– ikkuh
Nov 15 '18 at 14:24
Oooh, I didn't see that. Thanks.
– ukrutt
Nov 16 '18 at 15:40
Oooh, I didn't see that. Thanks.
– ukrutt
Nov 16 '18 at 15:40
add a comment |
1 reverse the key value,
2 sort the new key which is the value
my solution is to sort the keys
sounds like cheating, but works:
first call something to reverse dict
for i in sort(numbermap.keys()):
print(i,numbermap[i])
This won't work, because the order of keys is not the order the OP wants.
– jonrsharpe
Nov 15 '18 at 14:26
if you sort the key alphabetically - it wont.four
beforeone
just to name one
– Patrick Artner
Nov 15 '18 at 14:26
just an idea, u can reverse the key value and order the new key which is the current value
– 陈海栋
Nov 15 '18 at 14:34
add a comment |
1 reverse the key value,
2 sort the new key which is the value
my solution is to sort the keys
sounds like cheating, but works:
first call something to reverse dict
for i in sort(numbermap.keys()):
print(i,numbermap[i])
This won't work, because the order of keys is not the order the OP wants.
– jonrsharpe
Nov 15 '18 at 14:26
if you sort the key alphabetically - it wont.four
beforeone
just to name one
– Patrick Artner
Nov 15 '18 at 14:26
just an idea, u can reverse the key value and order the new key which is the current value
– 陈海栋
Nov 15 '18 at 14:34
add a comment |
1 reverse the key value,
2 sort the new key which is the value
my solution is to sort the keys
sounds like cheating, but works:
first call something to reverse dict
for i in sort(numbermap.keys()):
print(i,numbermap[i])
1 reverse the key value,
2 sort the new key which is the value
my solution is to sort the keys
sounds like cheating, but works:
first call something to reverse dict
for i in sort(numbermap.keys()):
print(i,numbermap[i])
edited Nov 15 '18 at 14:36
answered Nov 15 '18 at 14:26
陈海栋陈海栋
504
504
This won't work, because the order of keys is not the order the OP wants.
– jonrsharpe
Nov 15 '18 at 14:26
if you sort the key alphabetically - it wont.four
beforeone
just to name one
– Patrick Artner
Nov 15 '18 at 14:26
just an idea, u can reverse the key value and order the new key which is the current value
– 陈海栋
Nov 15 '18 at 14:34
add a comment |
This won't work, because the order of keys is not the order the OP wants.
– jonrsharpe
Nov 15 '18 at 14:26
if you sort the key alphabetically - it wont.four
beforeone
just to name one
– Patrick Artner
Nov 15 '18 at 14:26
just an idea, u can reverse the key value and order the new key which is the current value
– 陈海栋
Nov 15 '18 at 14:34
This won't work, because the order of keys is not the order the OP wants.
– jonrsharpe
Nov 15 '18 at 14:26
This won't work, because the order of keys is not the order the OP wants.
– jonrsharpe
Nov 15 '18 at 14:26
if you sort the key alphabetically - it wont.
four
before one
just to name one– Patrick Artner
Nov 15 '18 at 14:26
if you sort the key alphabetically - it wont.
four
before one
just to name one– Patrick Artner
Nov 15 '18 at 14:26
just an idea, u can reverse the key value and order the new key which is the current value
– 陈海栋
Nov 15 '18 at 14:34
just an idea, u can reverse the key value and order the new key which is the current value
– 陈海栋
Nov 15 '18 at 14:34
add a comment |
2
Python dictionaries aren't ordered data structures.
– jonrsharpe
Nov 15 '18 at 14:18
Any way to sorted as i read them?
– Venkat J
Nov 15 '18 at 14:19
1
Use an OrderedDict from the collections module if applicable in 2.6 - do you know about pythonclock.org ? Time to update. If you switch to 3.6 CPython or AnyPythong 3.7 you get insert order guranteed for nothing
– Patrick Artner
Nov 15 '18 at 14:22
1
Just saw: OrderedDict is not available for 2.6 - if you want them as you printed them, the only way to achieve it is to sort by value - but thats less then ideal because it does not reflect your insert order at all ... just put a
"ninetynine":99
in the first place ... its only by the values choosen that this fits.– Patrick Artner
Nov 15 '18 at 14:28
unfortunately, i have to do it in Python2.6.6, Is there anyway because OrderedDict is not available.
– Venkat J
Nov 15 '18 at 14:44