473,756 Members | 5,156 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to turn a list of tuples into a dictionary?

Let's say I've got a list of tuples, like so:

( ('a', '1'), ('b', '2'), ('c', '3')

And I want to turn it into a dictionary in which the first value of
each tuple is a key and the second value is a value, like so:

{ 'a' -'1', 'b' -'2', 'c' -'3' }

Is there a way to do this with a single line of code? Currently, I'm
doing it like this:

tuples = ( ('a', '1'), ('b', '2'), ('c', '3')
d = {}
for option,value in tuples: d[option] = value

Thanks,
--Steve

Feb 26 '08 #1
4 15708
On Feb 26, 11:00*am, mrstephengross <mrstevegr...@g mail.comwrote:
Let's say I've got a list of tuples, like so:

* ( ('a', '1'), ('b', '2'), ('c', '3')

And I want to turn it into a dictionary in which the first value of
each tuple is a key and the second value is a value, like so:

* { 'a' -'1', 'b' -'2', 'c' -'3' }

Is there a way to do this with a single line of code? Currently, I'm
doing it like this:

* tuples = * ( ('a', '1'), ('b', '2'), ('c', '3')
* d = {}
* for option,value in tuples: *d[option] = value

Thanks,
--Steve
I'd hand-code it manually, by linking a C extension. Or
dict( iterable ).

http://www.python.org/doc/current/li...cs.html#l2h-21
Feb 26 '08 #2
mrstephengross <mr**********@g mail.comwrites:
( ('a', '1'), ('b', '2'), ('c', '3')
I'm not trying to be snide, but have you tried looking in the manual?

See http://python.org/doc/lib and look at the section about built-in
types, if you want to know things about tuples or dictionaries.
Feb 26 '08 #3
On Tue, 26 Feb 2008 09:00:54 -0800 (PST)
mrstephengross <mr**********@g mail.comwrote:
Let's say I've got a list of tuples, like so:

( ('a', '1'), ('b', '2'), ('c', '3')

And I want to turn it into a dictionary in which the first value of
each tuple is a key and the second value is a value, like so:

{ 'a' -'1', 'b' -'2', 'c' -'3' }

Is there a way to do this with a single line of code? Currently, I'm
doing it like this:

tuples = ( ('a', '1'), ('b', '2'), ('c', '3')
d = {}
for option,value in tuples: d[option] = value
How about this?
d = dict(tuples)

--
D'Arcy J.M. Cain <da***@druid.ne t | Democracy is three wolves
http://www.druid.net/darcy/ | and a sheep voting on
+1 416 425 1212 (DoD#0082) (eNTP) | what's for dinner.
Feb 26 '08 #4
How about this?
d = dict(tuples)
Aha! I hadn't realized it could be so simple.

--Steve
Feb 26 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
14258
by: Nicolas Girard | last post by:
Hi, Forgive me if the answer is trivial, but could you tell me how to achieve the following: {k1:,k2:v3,...} --> ,,,...] The subtle point (at least to me) is to "flatten" values that are lists. Thanks in advance,
9
4082
by: Yomanium Yoth Taripoät II | last post by:
HI, 1) what are the differences between list and tuple? 2) how to concatenate tuple and list? no method, no opérator? 3) im looking the fucking manual, and cant add value in my tuple, when it already created :/ how to do it? thx.
3
2119
by: Thorsten Kampe | last post by:
I found out that I am rarely using tuples and almost always lists because of the more flexible usability of lists (methods, etc.) To my knowledge, the only fundamental difference between tuples and lists is that tuples are immutable, so if this is correct, than list are a superset of tuples, meaning lists can do everything tuples can do and more. Is there any advantage for using tuples? Are they "faster"? Consume less memory? When is...
10
2178
by: Philippe C. Martin | last post by:
Hi, I'm looking for an easy algorithm - maybe Python can help: I start with X lists which intial sort is based on list #1. I want to reverse sort list #1 and have all other lists sorted accordingly. Any idea is welcome.
24
3943
by: Mandus | last post by:
Hi there, inspired by a recent thread where the end of reduce/map/lambda in Python was discussed, I looked over some of my maps, and tried to convert them to list-comprehensions. This one I am not sure how to conver: Given three tuples of length n, b,i and d, I now do:
90
10816
by: Christoph Zwerschke | last post by:
Ok, the answer is easy: For historical reasons - built-in sets exist only since Python 2.4. Anyway, I was thinking about whether it would be possible and desirable to change the old behavior in future Python versions and let dict.keys() and dict.values() both return sets instead of lists. If d is a dict, code like: for x in d.keys():
7
5801
by: daniel | last post by:
is there any typical usage that shows their difference? thanks daniel
7
4443
by: Shafik | last post by:
Hello folks, I am an experienced programmer, but very new to python (2 days). I wanted to ask: what exactly is the difference between a tuple and a list? I'm sure there are some, but I can't seem to find a situation where I can use one but not the other. Thanks in advance, --Shafik
16
28728
by: Davy | last post by:
Hi all, We know that list cannot be used as key of dictionary. So, how to work around it? For example, there is random list like l=. Any suggestions are welcome! Best regards,
0
9287
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10046
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9886
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9722
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8723
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2677
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.