| | |
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
Views: 292 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for Python
address ansi backend beginner changecolor class code conversion coordinates copy curves customdialog dan08 dictionary digital directory dynamic edit events examples excel feet file float font format ftp function generator getvalue gui halp homework i/o iframe images import info input ip java line linux list lists loop mouse mysql newb number numbers output panel parsing path port prime print program programming projects py2exe pygame pyqt python queue random rational recursion recursive screensaverloopinactive scrolledtext server ssh stamp statictext string strings sudokusolver table terminal text thread threading time tkinter tlapse tuple tutorial type ubuntu unicode url urllib urllib2 variable whileloop windows write wxpython






