| | |
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:
Solved Threads: 0
Ideally I'd like to do it with a list comprehension. Something like this 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)
result = [(key, value)for key in dict.keys(), value in dict.values()]
EDIT:
This seems like what I want to do, but it doesn't work:
Python Syntax (Toggle Plain Text)
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.
0
#2 Nov 4th, 2009
Take a look at the final lines of this code snippet:
http://www.daniweb.com/code/snippet216747.html
http://www.daniweb.com/code/snippet216747.html
May 'the Google' be with you!
•
•
Join Date: Jul 2009
Posts: 31
Reputation:
Solved Threads: 0
0
#3 Nov 4th, 2009
•
•
•
•
Take a look at the final lines of this code snippet:
http://www.daniweb.com/code/snippet216747.html
I realized that I can just use .items() (feels silly)
0
#5 Nov 5th, 2009
•
•
•
•
...Python Syntax (Toggle Plain Text)
result = [(key, value)for key in dict.keys(), value in dict.values()]
Python Syntax (Toggle Plain Text)
result = [(dict.keys[i], dict.values[i]) for i in range(1, len(dict))]
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)
d = {'a':1, 'b':2, 'c':3} for idx in xrange(15): y += my_list[idx] if y == 256: break list('123456890') dict(some_iterable)
Last edited by jlm699; Nov 5th, 2009 at 4:36 pm.
![]() |
Similar Threads
- Python dictionary output it as it was input (Python)
- Which is faster, a Dictionary Lookup or an Index Operation? (Python)
- A few questions from a newbie with Tk (Python)
- Sorting a dictionary (Python)
- Odd Dictionary Behavior (Python)
- Dictionary Key Error Py 3.0 (Python)
- sorting dictionary for graphics (Python)
- how to define a dictionary in python (Python)
- Invert a dictionary (Python)
- sourcing a python config file? (Python)
Other Threads in the Python Forum
- Previous Thread: python tic tac toe game.
- Next Thread: while loop in python
| Thread Tools | Search this Thread |
Tag cloud for Python
accessdenied apache application argv beginner book change code color dictionary dynamic edit editing enter examples excel file filename float format ftp function gui homework import inches input java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysql newb number numbers numeric output parameters parsing path port prime program programming projects py2exe pygame pyopengl pyqt python random recursion recursive redirect remote reverse rpg scrolledtext search server session simple smtp software sprite ssh statictext string strings syntax table tennis terminal text thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable windows wordgame wxpython






