Getting the first string from a tuple which is a key of a dict
up vote
2
down vote
favorite
I have a following python dictionary:
lines = {('mid', 'north'): None,
('south', 'mid'): None,
('south', 'north'): None,
}
what I am trying to achieve is to get the first string or/and second string of one of the keys of the lines
dictionary.
so lines.keys()
gives me dict_keys([('mid', 'north'), ('south', 'mid'), ('south', 'north')])
what I would like to have is something like this:
lines.keys()[0][0]
'mid'
ofc it is not working like this but is there a function or a command which gives me the first string of the tuple ('mid', 'north')
, as an output? Keep in mind ('mid', 'north')
is the key of the dictionary.
python dictionary tuples key
add a comment |
up vote
2
down vote
favorite
I have a following python dictionary:
lines = {('mid', 'north'): None,
('south', 'mid'): None,
('south', 'north'): None,
}
what I am trying to achieve is to get the first string or/and second string of one of the keys of the lines
dictionary.
so lines.keys()
gives me dict_keys([('mid', 'north'), ('south', 'mid'), ('south', 'north')])
what I would like to have is something like this:
lines.keys()[0][0]
'mid'
ofc it is not working like this but is there a function or a command which gives me the first string of the tuple ('mid', 'north')
, as an output? Keep in mind ('mid', 'north')
is the key of the dictionary.
python dictionary tuples key
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I have a following python dictionary:
lines = {('mid', 'north'): None,
('south', 'mid'): None,
('south', 'north'): None,
}
what I am trying to achieve is to get the first string or/and second string of one of the keys of the lines
dictionary.
so lines.keys()
gives me dict_keys([('mid', 'north'), ('south', 'mid'), ('south', 'north')])
what I would like to have is something like this:
lines.keys()[0][0]
'mid'
ofc it is not working like this but is there a function or a command which gives me the first string of the tuple ('mid', 'north')
, as an output? Keep in mind ('mid', 'north')
is the key of the dictionary.
python dictionary tuples key
I have a following python dictionary:
lines = {('mid', 'north'): None,
('south', 'mid'): None,
('south', 'north'): None,
}
what I am trying to achieve is to get the first string or/and second string of one of the keys of the lines
dictionary.
so lines.keys()
gives me dict_keys([('mid', 'north'), ('south', 'mid'), ('south', 'north')])
what I would like to have is something like this:
lines.keys()[0][0]
'mid'
ofc it is not working like this but is there a function or a command which gives me the first string of the tuple ('mid', 'north')
, as an output? Keep in mind ('mid', 'north')
is the key of the dictionary.
python dictionary tuples key
python dictionary tuples key
asked Nov 21 at 12:26
oakca
15811
15811
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
You can convert dict_keys
to tuple, since it is iterable:
>>> k = tuple(lines.keys())
>>> k[0][0]
'mid'
This looks like the easiest solution ty. And it works. Btw when you dok = list(lines.keys())
and thenk[0][0]
givesmid
and not'mid'
, can u explain why is that? and also add to your answer?
– oakca
Nov 21 at 12:37
k[0][0]
contains the stringmid
. In python you would write that as'mid'
(or"mid"
), but if you doprint(k[0][0])
it would just output string's content (mid
). If you want to obtain the string representation, doprint(repr(k[0][0]))
, and it will print'mid'
. The interactive console gives you the string representation by default.
– fferri
Nov 21 at 12:39
add a comment |
up vote
0
down vote
You need to convert your keys to list, and since dict will be in random order you need to sort it.
lines = {('mid', 'north'): None,
('south', 'mid'): None,
('south', 'north'): None,
}
print(list(sorted(lines.keys()))[0][0]) # -> mid
Looks nice, but why sorted?
– oakca
Nov 21 at 12:30
I don't think it will be in random order, but it will be the order I set them. so[0]
is always('mid', 'north')
, and[1]
is('south', 'mid')
, etc...
– oakca
Nov 21 at 12:33
No it won't tryprint(lines.keys())
running that couple of times.
– Filip Młynarski
Nov 21 at 12:34
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
You can convert dict_keys
to tuple, since it is iterable:
>>> k = tuple(lines.keys())
>>> k[0][0]
'mid'
This looks like the easiest solution ty. And it works. Btw when you dok = list(lines.keys())
and thenk[0][0]
givesmid
and not'mid'
, can u explain why is that? and also add to your answer?
– oakca
Nov 21 at 12:37
k[0][0]
contains the stringmid
. In python you would write that as'mid'
(or"mid"
), but if you doprint(k[0][0])
it would just output string's content (mid
). If you want to obtain the string representation, doprint(repr(k[0][0]))
, and it will print'mid'
. The interactive console gives you the string representation by default.
– fferri
Nov 21 at 12:39
add a comment |
up vote
0
down vote
accepted
You can convert dict_keys
to tuple, since it is iterable:
>>> k = tuple(lines.keys())
>>> k[0][0]
'mid'
This looks like the easiest solution ty. And it works. Btw when you dok = list(lines.keys())
and thenk[0][0]
givesmid
and not'mid'
, can u explain why is that? and also add to your answer?
– oakca
Nov 21 at 12:37
k[0][0]
contains the stringmid
. In python you would write that as'mid'
(or"mid"
), but if you doprint(k[0][0])
it would just output string's content (mid
). If you want to obtain the string representation, doprint(repr(k[0][0]))
, and it will print'mid'
. The interactive console gives you the string representation by default.
– fferri
Nov 21 at 12:39
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
You can convert dict_keys
to tuple, since it is iterable:
>>> k = tuple(lines.keys())
>>> k[0][0]
'mid'
You can convert dict_keys
to tuple, since it is iterable:
>>> k = tuple(lines.keys())
>>> k[0][0]
'mid'
answered Nov 21 at 12:33
fferri
11.5k22251
11.5k22251
This looks like the easiest solution ty. And it works. Btw when you dok = list(lines.keys())
and thenk[0][0]
givesmid
and not'mid'
, can u explain why is that? and also add to your answer?
– oakca
Nov 21 at 12:37
k[0][0]
contains the stringmid
. In python you would write that as'mid'
(or"mid"
), but if you doprint(k[0][0])
it would just output string's content (mid
). If you want to obtain the string representation, doprint(repr(k[0][0]))
, and it will print'mid'
. The interactive console gives you the string representation by default.
– fferri
Nov 21 at 12:39
add a comment |
This looks like the easiest solution ty. And it works. Btw when you dok = list(lines.keys())
and thenk[0][0]
givesmid
and not'mid'
, can u explain why is that? and also add to your answer?
– oakca
Nov 21 at 12:37
k[0][0]
contains the stringmid
. In python you would write that as'mid'
(or"mid"
), but if you doprint(k[0][0])
it would just output string's content (mid
). If you want to obtain the string representation, doprint(repr(k[0][0]))
, and it will print'mid'
. The interactive console gives you the string representation by default.
– fferri
Nov 21 at 12:39
This looks like the easiest solution ty. And it works. Btw when you do
k = list(lines.keys())
and then k[0][0]
gives mid
and not 'mid'
, can u explain why is that? and also add to your answer?– oakca
Nov 21 at 12:37
This looks like the easiest solution ty. And it works. Btw when you do
k = list(lines.keys())
and then k[0][0]
gives mid
and not 'mid'
, can u explain why is that? and also add to your answer?– oakca
Nov 21 at 12:37
k[0][0]
contains the string mid
. In python you would write that as 'mid'
(or "mid"
), but if you do print(k[0][0])
it would just output string's content (mid
). If you want to obtain the string representation, do print(repr(k[0][0]))
, and it will print 'mid'
. The interactive console gives you the string representation by default.– fferri
Nov 21 at 12:39
k[0][0]
contains the string mid
. In python you would write that as 'mid'
(or "mid"
), but if you do print(k[0][0])
it would just output string's content (mid
). If you want to obtain the string representation, do print(repr(k[0][0]))
, and it will print 'mid'
. The interactive console gives you the string representation by default.– fferri
Nov 21 at 12:39
add a comment |
up vote
0
down vote
You need to convert your keys to list, and since dict will be in random order you need to sort it.
lines = {('mid', 'north'): None,
('south', 'mid'): None,
('south', 'north'): None,
}
print(list(sorted(lines.keys()))[0][0]) # -> mid
Looks nice, but why sorted?
– oakca
Nov 21 at 12:30
I don't think it will be in random order, but it will be the order I set them. so[0]
is always('mid', 'north')
, and[1]
is('south', 'mid')
, etc...
– oakca
Nov 21 at 12:33
No it won't tryprint(lines.keys())
running that couple of times.
– Filip Młynarski
Nov 21 at 12:34
add a comment |
up vote
0
down vote
You need to convert your keys to list, and since dict will be in random order you need to sort it.
lines = {('mid', 'north'): None,
('south', 'mid'): None,
('south', 'north'): None,
}
print(list(sorted(lines.keys()))[0][0]) # -> mid
Looks nice, but why sorted?
– oakca
Nov 21 at 12:30
I don't think it will be in random order, but it will be the order I set them. so[0]
is always('mid', 'north')
, and[1]
is('south', 'mid')
, etc...
– oakca
Nov 21 at 12:33
No it won't tryprint(lines.keys())
running that couple of times.
– Filip Młynarski
Nov 21 at 12:34
add a comment |
up vote
0
down vote
up vote
0
down vote
You need to convert your keys to list, and since dict will be in random order you need to sort it.
lines = {('mid', 'north'): None,
('south', 'mid'): None,
('south', 'north'): None,
}
print(list(sorted(lines.keys()))[0][0]) # -> mid
You need to convert your keys to list, and since dict will be in random order you need to sort it.
lines = {('mid', 'north'): None,
('south', 'mid'): None,
('south', 'north'): None,
}
print(list(sorted(lines.keys()))[0][0]) # -> mid
edited Nov 21 at 12:30
answered Nov 21 at 12:29
Filip Młynarski
1,351111
1,351111
Looks nice, but why sorted?
– oakca
Nov 21 at 12:30
I don't think it will be in random order, but it will be the order I set them. so[0]
is always('mid', 'north')
, and[1]
is('south', 'mid')
, etc...
– oakca
Nov 21 at 12:33
No it won't tryprint(lines.keys())
running that couple of times.
– Filip Młynarski
Nov 21 at 12:34
add a comment |
Looks nice, but why sorted?
– oakca
Nov 21 at 12:30
I don't think it will be in random order, but it will be the order I set them. so[0]
is always('mid', 'north')
, and[1]
is('south', 'mid')
, etc...
– oakca
Nov 21 at 12:33
No it won't tryprint(lines.keys())
running that couple of times.
– Filip Młynarski
Nov 21 at 12:34
Looks nice, but why sorted?
– oakca
Nov 21 at 12:30
Looks nice, but why sorted?
– oakca
Nov 21 at 12:30
I don't think it will be in random order, but it will be the order I set them. so
[0]
is always ('mid', 'north')
, and [1]
is ('south', 'mid')
, etc...– oakca
Nov 21 at 12:33
I don't think it will be in random order, but it will be the order I set them. so
[0]
is always ('mid', 'north')
, and [1]
is ('south', 'mid')
, etc...– oakca
Nov 21 at 12:33
No it won't try
print(lines.keys())
running that couple of times.– Filip Młynarski
Nov 21 at 12:34
No it won't try
print(lines.keys())
running that couple of times.– Filip Młynarski
Nov 21 at 12:34
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53412008%2fgetting-the-first-string-from-a-tuple-which-is-a-key-of-a-dict%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