944,134 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 1083
  • Python RSS
Nov 4th, 2009
0

Converting a dictionary into a list of (key, value) tuples

Expand Post »
Ideally I'd like to do it with a list comprehension. Something like this
Python Syntax (Toggle Plain Text)
  1. result = [(key, value)for key in dict.keys(), value in dict.values()]
but clearly that isn't the right format. Any suggestions?

EDIT:

This seems like what I want to do, but it doesn't work:
Python Syntax (Toggle Plain Text)
  1. result = [(dict.keys[i], dict.values[i]) for i in range(1, len(dict))]
Last edited by tdeck; Nov 4th, 2009 at 6:41 pm.
Similar Threads
Reputation Points: 17
Solved Threads: 0
Light Poster
tdeck is offline Offline
31 posts
since Jul 2009
Nov 4th, 2009
0
Re: Converting a dictionary into a list of (key, value) tuples
Take a look at the final lines of this code snippet:
http://www.daniweb.com/code/snippet216747.html
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 4th, 2009
0
Re: Converting a dictionary into a list of (key, value) tuples
Click to Expand / Collapse  Quote originally posted by vegaseat ...
Take a look at the final lines of this code snippet:
http://www.daniweb.com/code/snippet216747.html
Thanks for that. Weirdly enough, what I'm doing is actually related to counting word frequencies.

I realized that I can just use .items() (feels silly)
Reputation Points: 17
Solved Threads: 0
Light Poster
tdeck is offline Offline
31 posts
since Jul 2009
Nov 5th, 2009
0
Re: Converting a dictionary into a list of (key, value) tuples
If you are brave enough to ask questions, then you are not silly!
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Nov 5th, 2009
0
Re: Converting a dictionary into a list of (key, value) tuples
Click to Expand / Collapse  Quote originally posted by tdeck ...
Python Syntax (Toggle Plain Text)
  1. result = [(key, value)for key in dict.keys(), value in dict.values()]
...
Python Syntax (Toggle Plain Text)
  1. result = [(dict.keys[i], dict.values[i]) for i in range(1, len(dict))]
Just please note that in Python, we use the built-in function dict() to convert certain objects to a dictionary. If you used the above code, you would lose this ability as you're overwriting that built-in function with your list!!

EDIT: Notice how dict is highlighted by the syntax highlighting above like the following built-ins (reserved keywords)
python Syntax (Toggle Plain Text)
  1. d = {'a':1, 'b':2, 'c':3}
  2. for idx in xrange(15):
  3. y += my_list[idx]
  4. if y == 256:
  5. break
  6. list('123456890')
  7. dict(some_iterable)
Last edited by jlm699; Nov 5th, 2009 at 4:36 pm.
Reputation Points: 355
Solved Threads: 292
Veteran Poster
jlm699 is offline Offline
1,102 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: python tic tac toe game.
Next Thread in Python Forum Timeline: while loop in python





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC