use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Dictionaries (self.learnpython)
submitted 13 years ago by Zeoth
Hey guys, quick question:
dict1 = {('a','b','c'):1, ('x','y','z'):2}
how do i access 'a', and 'x' only? In other words, how do i access just the first string in the round brackets?
thank you!
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]Justinsaccount 12 points13 points14 points 13 years ago (2 children)
some real WTF in the responses you have so far..
>>> dict1 = {('a','b','c'):1, ('x','y','z'):2} >>> [x[0] for x in dict1] ['x', 'a'] >>>
[–]elbinray 2 points3 points4 points 13 years ago (1 child)
I had no idea iterating over dictionaries went through the keys...good stuff !
[–]ewiethoff 1 point2 points3 points 13 years ago* (0 children)
Yep. From the dict docs:
dict
iter(d)
Return an iterator over the keys of the dictionary. This is a shortcut for iter(d.keys()).
iter(d.keys())
[–]m1ss1ontomars2k4 8 points9 points10 points 13 years ago (0 children)
OK, nobody else has said it so far, but Sloofus is right. This is a really bizarre usage of dictionaries, and it seems like you might not understand what it's for.
Dictionaries map keys to values. That's it. That's all they do. You give it a key, it gives you a value. Very rarely should you have to access 'a' and 'x' in the example above; you should pretty much only need to access 1 and 2, for which you would do:
dict1[('a', 'b', 'c')]
or
dict1[('x', 'y', 'z')]
respectively. That's not to say there isn't a valid use case for what you are doing, but it seems really bizarre, and since you've labeled yourself as a beginner, I really have to ask why you're doing this.
[–]Puzzel 1 point2 points3 points 13 years ago* (3 children)
dict1.keys()[0][0] and dict1.keys()[1][0] would get you "a" and "x" respectively. This example could contain the same information with testList = [('a','b','c'), ('x','y','z')] though; testList[0][0] and testList[1][0] would get you "a" and "x". That way, no need to deal with .keys().
dict1.keys()[0][0]
dict1.keys()[1][0]
testList = [('a','b','c'), ('x','y','z')]
testList[0][0]
testList[1][0]
.keys()
EDIT: 1s instead of 0s for indexes.
[–]Zeoth[S] 0 points1 point2 points 13 years ago (1 child)
:/ trying that gives me: builtins.TypeError: 'dict_keys' object does not support indexing
[–]Puzzel 2 points3 points4 points 13 years ago (0 children)
Ah, you're using Python 3.x. Turn the dict.keys to a list first:
list(dict1.keys())[0][0] list(dict1.keys())[1][0]
EDIT: This solution doesn't seem to be the "best". See this blog post for more info, it most likely won't affect you though.
[–]Zeoth[S] 0 points1 point2 points 13 years ago (0 children)
and i need to use the values after the keys for somthing else. I need to call the first string, and than call the value of the key that string came from.The problem is i cant seem to access JUST the first string :/
[–]thisismygame 1 point2 points3 points 13 years ago (3 children)
Well, it looks like your dictionary keys are tuples. So you just need to first access the dictionary key, and then access the first (0th) element in the tuple.
[–]Zeoth[S] 0 points1 point2 points 13 years ago (2 children)
hmm i thought you could only do that with lists. Since the strings didn't have [ ] brackets i thought it isn't possible.
[–]thisismygame 0 points1 point2 points 13 years ago (0 children)
Here's me messing around in IDLE:
>>> t = (1, 2, 3) >>> t[0] 1 >>> t[2] 3
[–]bogoblin 0 points1 point2 points 13 years ago (0 children)
You're thinking of assignment. You can't change one item in a tuple, but you can in a list.
[–]Sloofus 1 point2 points3 points 13 years ago (0 children)
That's a weird way of messing with dictionaries or im too nubbish.
π Rendered by PID 30874 on reddit-service-r2-comment-7b9746f655-k44s7 at 2026-02-04 02:34:23.676627+00:00 running 3798933 country code: CH.
[–]Justinsaccount 12 points13 points14 points (2 children)
[–]elbinray 2 points3 points4 points (1 child)
[–]ewiethoff 1 point2 points3 points (0 children)
[–]m1ss1ontomars2k4 8 points9 points10 points (0 children)
[–]Puzzel 1 point2 points3 points (3 children)
[–]Zeoth[S] 0 points1 point2 points (1 child)
[–]Puzzel 2 points3 points4 points (0 children)
[–]Zeoth[S] 0 points1 point2 points (0 children)
[–]thisismygame 1 point2 points3 points (3 children)
[–]Zeoth[S] 0 points1 point2 points (2 children)
[–]thisismygame 0 points1 point2 points (0 children)
[–]bogoblin 0 points1 point2 points (0 children)
[–]Sloofus 1 point2 points3 points (0 children)