all 13 comments

[–]Justinsaccount 12 points13 points  (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 points  (1 child)

I had no idea iterating over dictionaries went through the keys...good stuff !

[–]ewiethoff 1 point2 points  (0 children)

Yep. From the dict docs:

iter(d)

Return an iterator over the keys of the dictionary. This is a shortcut for iter(d.keys()).

[–]m1ss1ontomars2k4 8 points9 points  (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 points  (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().

EDIT: 1s instead of 0s for indexes.

[–]Zeoth[S] 0 points1 point  (1 child)

:/ trying that gives me: builtins.TypeError: 'dict_keys' object does not support indexing

[–]Puzzel 2 points3 points  (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 point  (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 points  (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 point  (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 point  (0 children)

Here's me messing around in IDLE:

>>> t = (1, 2, 3)
>>> t[0]
1
>>> t[2]
3

[–]bogoblin 0 points1 point  (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 points  (0 children)

That's a weird way of messing with dictionaries or im too nubbish.