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

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Jul 2009
Posts: 31
Reputation: tdeck is an unknown quantity at this point 
Solved Threads: 0
tdeck tdeck is offline Offline
Light Poster

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

 
0
  #1
Nov 4th, 2009
Ideally I'd like to do it with a list comprehension. Something like this
  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:
  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,113
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 944
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #2
Nov 4th, 2009
Take a look at the final lines of this code snippet:
http://www.daniweb.com/code/snippet216747.html
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 31
Reputation: tdeck is an unknown quantity at this point 
Solved Threads: 0
tdeck tdeck is offline Offline
Light Poster
 
0
  #3
Nov 4th, 2009
Originally Posted by vegaseat View Post
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)
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 4,113
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 944
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite
 
0
  #4
Nov 5th, 2009
If you are brave enough to ask questions, then you are not silly!
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,067
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 267
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is
 
0
  #5
Nov 5th, 2009
Originally Posted by tdeck View Post
  1. result = [(key, value)for key in dict.keys(), value in dict.values()]
...
  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)
  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.
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC