How to change the first element in a tuple that is a key in a dictionary?
I need to set the first element of a tuple which is a key in a dictionary to be current
.
{('a', '0'): 'c',
('b', '0'): 'd',}
I need to set 'a'
as current
variable before looping the code.
How can I do this?
python dictionary tuples
add a comment |
I need to set the first element of a tuple which is a key in a dictionary to be current
.
{('a', '0'): 'c',
('b', '0'): 'd',}
I need to set 'a'
as current
variable before looping the code.
How can I do this?
python dictionary tuples
3
Can you elaborate on what you are doing? Are you trying to use 'a' as a variable name, or just remember that the last time you checked, 'a' was valid, or what?
– Austin Hastings
Nov 22 '18 at 16:09
add a comment |
I need to set the first element of a tuple which is a key in a dictionary to be current
.
{('a', '0'): 'c',
('b', '0'): 'd',}
I need to set 'a'
as current
variable before looping the code.
How can I do this?
python dictionary tuples
I need to set the first element of a tuple which is a key in a dictionary to be current
.
{('a', '0'): 'c',
('b', '0'): 'd',}
I need to set 'a'
as current
variable before looping the code.
How can I do this?
python dictionary tuples
python dictionary tuples
edited Nov 22 '18 at 18:23
Eugene Yarmash
83.7k22177260
83.7k22177260
asked Nov 22 '18 at 16:02
pollywogzpollywogz
175
175
3
Can you elaborate on what you are doing? Are you trying to use 'a' as a variable name, or just remember that the last time you checked, 'a' was valid, or what?
– Austin Hastings
Nov 22 '18 at 16:09
add a comment |
3
Can you elaborate on what you are doing? Are you trying to use 'a' as a variable name, or just remember that the last time you checked, 'a' was valid, or what?
– Austin Hastings
Nov 22 '18 at 16:09
3
3
Can you elaborate on what you are doing? Are you trying to use 'a' as a variable name, or just remember that the last time you checked, 'a' was valid, or what?
– Austin Hastings
Nov 22 '18 at 16:09
Can you elaborate on what you are doing? Are you trying to use 'a' as a variable name, or just remember that the last time you checked, 'a' was valid, or what?
– Austin Hastings
Nov 22 '18 at 16:09
add a comment |
3 Answers
3
active
oldest
votes
You can't modify the key itself because it's an immutable object. Instead, replace the dictionary key with a new one, e.g.:
key1, key2 = ('a', '0'), (current, 0)
d[key2] = d[key1]
del d[key1]
or in one step:
d[key2] = d.pop(key1)
add a comment |
If you want to loop through all your dictionaries to update them only by the first item in your tuple key.
d = {('a', '0'): 'c', ('b', '0'): 'd',}
for key in d:
new_key = key[0]
d[new_key] = d.pop(key)
d
>>{'a': 'c', 'b': 'd'}
Essentially here you are deleting the old key and replacing it with a new one where you take index 0
of your tuple to be the new key.
add a comment |
Are you trying to access 'a' from the dictionary?
If you are then you can do the following:
list(d.keys())[0][0]
>>> a
However; if the above is not what is required from you but the below is.
{('a', '0'): 'a', ('b', '0'): 'd'}
You can accomplish this with lambda or with list comprehension:
# Lambda
d[('a', '0')] = list(map(lambda k: k[0], d))[0]
print(d)
>>> {('a', '0'): 'a', ('b', '0'): 'd'}
# list comprehension
d[('a', '0')] = [k[0] for k in d][0]
>>> {('a', '0'): 'a', ('b', '0'): 'd'}
Or are you looking to achieve something as this:
{('b', '0'): 'd', ('c', '0'): 'a'}
Again, with lambda or with list comprehension:
# Lambda
d[('a', '0')] = list(map(lambda k: k[0], d))[0]
d[('c', '0')] = d.pop(('a', '0'))
print(d)
>>> {('b', '0'): 'd', ('c', '0'): 'a'}
# list comprehension
d[('a', '0')] = [k[0] for k in d][0]
d[('c', '0')] = d.pop(('a', '0'))
print(d)
>>> {('b', '0'): 'd', ('c', '0'): 'a'}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434651%2fhow-to-change-the-first-element-in-a-tuple-that-is-a-key-in-a-dictionary%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can't modify the key itself because it's an immutable object. Instead, replace the dictionary key with a new one, e.g.:
key1, key2 = ('a', '0'), (current, 0)
d[key2] = d[key1]
del d[key1]
or in one step:
d[key2] = d.pop(key1)
add a comment |
You can't modify the key itself because it's an immutable object. Instead, replace the dictionary key with a new one, e.g.:
key1, key2 = ('a', '0'), (current, 0)
d[key2] = d[key1]
del d[key1]
or in one step:
d[key2] = d.pop(key1)
add a comment |
You can't modify the key itself because it's an immutable object. Instead, replace the dictionary key with a new one, e.g.:
key1, key2 = ('a', '0'), (current, 0)
d[key2] = d[key1]
del d[key1]
or in one step:
d[key2] = d.pop(key1)
You can't modify the key itself because it's an immutable object. Instead, replace the dictionary key with a new one, e.g.:
key1, key2 = ('a', '0'), (current, 0)
d[key2] = d[key1]
del d[key1]
or in one step:
d[key2] = d.pop(key1)
edited Dec 12 '18 at 13:41
answered Nov 22 '18 at 16:05
Eugene YarmashEugene Yarmash
83.7k22177260
83.7k22177260
add a comment |
add a comment |
If you want to loop through all your dictionaries to update them only by the first item in your tuple key.
d = {('a', '0'): 'c', ('b', '0'): 'd',}
for key in d:
new_key = key[0]
d[new_key] = d.pop(key)
d
>>{'a': 'c', 'b': 'd'}
Essentially here you are deleting the old key and replacing it with a new one where you take index 0
of your tuple to be the new key.
add a comment |
If you want to loop through all your dictionaries to update them only by the first item in your tuple key.
d = {('a', '0'): 'c', ('b', '0'): 'd',}
for key in d:
new_key = key[0]
d[new_key] = d.pop(key)
d
>>{'a': 'c', 'b': 'd'}
Essentially here you are deleting the old key and replacing it with a new one where you take index 0
of your tuple to be the new key.
add a comment |
If you want to loop through all your dictionaries to update them only by the first item in your tuple key.
d = {('a', '0'): 'c', ('b', '0'): 'd',}
for key in d:
new_key = key[0]
d[new_key] = d.pop(key)
d
>>{'a': 'c', 'b': 'd'}
Essentially here you are deleting the old key and replacing it with a new one where you take index 0
of your tuple to be the new key.
If you want to loop through all your dictionaries to update them only by the first item in your tuple key.
d = {('a', '0'): 'c', ('b', '0'): 'd',}
for key in d:
new_key = key[0]
d[new_key] = d.pop(key)
d
>>{'a': 'c', 'b': 'd'}
Essentially here you are deleting the old key and replacing it with a new one where you take index 0
of your tuple to be the new key.
answered Nov 22 '18 at 16:07
BernardLBernardL
2,3581929
2,3581929
add a comment |
add a comment |
Are you trying to access 'a' from the dictionary?
If you are then you can do the following:
list(d.keys())[0][0]
>>> a
However; if the above is not what is required from you but the below is.
{('a', '0'): 'a', ('b', '0'): 'd'}
You can accomplish this with lambda or with list comprehension:
# Lambda
d[('a', '0')] = list(map(lambda k: k[0], d))[0]
print(d)
>>> {('a', '0'): 'a', ('b', '0'): 'd'}
# list comprehension
d[('a', '0')] = [k[0] for k in d][0]
>>> {('a', '0'): 'a', ('b', '0'): 'd'}
Or are you looking to achieve something as this:
{('b', '0'): 'd', ('c', '0'): 'a'}
Again, with lambda or with list comprehension:
# Lambda
d[('a', '0')] = list(map(lambda k: k[0], d))[0]
d[('c', '0')] = d.pop(('a', '0'))
print(d)
>>> {('b', '0'): 'd', ('c', '0'): 'a'}
# list comprehension
d[('a', '0')] = [k[0] for k in d][0]
d[('c', '0')] = d.pop(('a', '0'))
print(d)
>>> {('b', '0'): 'd', ('c', '0'): 'a'}
add a comment |
Are you trying to access 'a' from the dictionary?
If you are then you can do the following:
list(d.keys())[0][0]
>>> a
However; if the above is not what is required from you but the below is.
{('a', '0'): 'a', ('b', '0'): 'd'}
You can accomplish this with lambda or with list comprehension:
# Lambda
d[('a', '0')] = list(map(lambda k: k[0], d))[0]
print(d)
>>> {('a', '0'): 'a', ('b', '0'): 'd'}
# list comprehension
d[('a', '0')] = [k[0] for k in d][0]
>>> {('a', '0'): 'a', ('b', '0'): 'd'}
Or are you looking to achieve something as this:
{('b', '0'): 'd', ('c', '0'): 'a'}
Again, with lambda or with list comprehension:
# Lambda
d[('a', '0')] = list(map(lambda k: k[0], d))[0]
d[('c', '0')] = d.pop(('a', '0'))
print(d)
>>> {('b', '0'): 'd', ('c', '0'): 'a'}
# list comprehension
d[('a', '0')] = [k[0] for k in d][0]
d[('c', '0')] = d.pop(('a', '0'))
print(d)
>>> {('b', '0'): 'd', ('c', '0'): 'a'}
add a comment |
Are you trying to access 'a' from the dictionary?
If you are then you can do the following:
list(d.keys())[0][0]
>>> a
However; if the above is not what is required from you but the below is.
{('a', '0'): 'a', ('b', '0'): 'd'}
You can accomplish this with lambda or with list comprehension:
# Lambda
d[('a', '0')] = list(map(lambda k: k[0], d))[0]
print(d)
>>> {('a', '0'): 'a', ('b', '0'): 'd'}
# list comprehension
d[('a', '0')] = [k[0] for k in d][0]
>>> {('a', '0'): 'a', ('b', '0'): 'd'}
Or are you looking to achieve something as this:
{('b', '0'): 'd', ('c', '0'): 'a'}
Again, with lambda or with list comprehension:
# Lambda
d[('a', '0')] = list(map(lambda k: k[0], d))[0]
d[('c', '0')] = d.pop(('a', '0'))
print(d)
>>> {('b', '0'): 'd', ('c', '0'): 'a'}
# list comprehension
d[('a', '0')] = [k[0] for k in d][0]
d[('c', '0')] = d.pop(('a', '0'))
print(d)
>>> {('b', '0'): 'd', ('c', '0'): 'a'}
Are you trying to access 'a' from the dictionary?
If you are then you can do the following:
list(d.keys())[0][0]
>>> a
However; if the above is not what is required from you but the below is.
{('a', '0'): 'a', ('b', '0'): 'd'}
You can accomplish this with lambda or with list comprehension:
# Lambda
d[('a', '0')] = list(map(lambda k: k[0], d))[0]
print(d)
>>> {('a', '0'): 'a', ('b', '0'): 'd'}
# list comprehension
d[('a', '0')] = [k[0] for k in d][0]
>>> {('a', '0'): 'a', ('b', '0'): 'd'}
Or are you looking to achieve something as this:
{('b', '0'): 'd', ('c', '0'): 'a'}
Again, with lambda or with list comprehension:
# Lambda
d[('a', '0')] = list(map(lambda k: k[0], d))[0]
d[('c', '0')] = d.pop(('a', '0'))
print(d)
>>> {('b', '0'): 'd', ('c', '0'): 'a'}
# list comprehension
d[('a', '0')] = [k[0] for k in d][0]
d[('c', '0')] = d.pop(('a', '0'))
print(d)
>>> {('b', '0'): 'd', ('c', '0'): 'a'}
edited Nov 23 '18 at 10:44
answered Nov 22 '18 at 19:13
Victor SVictor S
1668
1668
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434651%2fhow-to-change-the-first-element-in-a-tuple-that-is-a-key-in-a-dictionary%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
Can you elaborate on what you are doing? Are you trying to use 'a' as a variable name, or just remember that the last time you checked, 'a' was valid, or what?
– Austin Hastings
Nov 22 '18 at 16:09