473,396 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Combine two dictionaries into a list of 3-tuples

Hi all,

Continuing the search for interesting challenges with lists, tuples and
dictionaries I am looking for a way to do the following.

one = {
'a' : 1,
'b' : 2,
'c' : 3
}

two = {
'a' : [4,5,6],
'b' : [8,9,10],
'c' : [11,12,13]
}

The goal is

[('a', 1, [4,5,6]), ('b', 2, [8,9,10]), ('c', 3, [11,12,13])]
My attempts so far:

[(t, c, l) for t, c in one.items() if (t, l) in two.items()]

gives me a 'l is not defined'

---------------

[(t, c, l) for t, c in one.items() for (t, l) in two.items()]

gives me a lot more than I need :-)

---------------

Many thanks in advance!

Cheers,
-- Nickolay
Jul 18 '05 #1
3 1847
How about
[(k, v, two[v]) for k, v in one.items()]
or
[(k, v, two[v]) for k, v in one.items() if k in two]
or
[(k, v, two.get(v, None)) for k, v in one.items()]
depending on what you want to do when a key doesn't exist in "two"

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQFBkoRZJd01MZaTXX0RAvpNAJ4vYvie7kADHprwdOjuoa 605/8eRwCeIBwX
vyDcQKHMKSgRsgB1Y/TtzAo=
=u8x/
-----END PGP SIGNATURE-----

Jul 18 '05 #2
Jeff Epler wrote:
How about
[(k, v, two[v]) for k, v in one.items()]
or
[(k, v, two[v]) for k, v in one.items() if k in two]
or
[(k, v, two.get(v, None)) for k, v in one.items()]
depending on what you want to do when a key doesn't exist in "two"

Jeff


I should have thought of that, it is sooo simple... :-)

Thanks again.

Cheers,
-- Nickolay
Jul 18 '05 #3
Nickolay Kolev wrote:
My attempts so far:
... [(t, c, l) for t, c in one.items() for (t, l) in two.items()]


Simply forget about items for two.
[(key, val1, two[key]) for key, val1 in one.items() if key in two]

--Scott David Daniels
Sc***********@Acm.Org
Jul 18 '05 #4

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

Similar topics

6
by: Narendra C. Tulpule | last post by:
Hi, if you know the Python internals, here is a newbie question for you. If I have a list with 100 elements, each element being a long string, is it more efficient to maintain it as a dictionary...
7
by: Kerry Neilson | last post by:
Hi, Really hung up on this one. I'm trying to get all the fields of a dictionary to be unique for each class: class A { my_dict = dict_entry = { 'key1':0, 'key2':0 } __init__(self): for...
8
by: Frohnhofer, James | last post by:
My initial problem was to initialize a bunch of dictionaries at the start of a function. I did not want to do def fn(): a = {} b = {} c = {} . . . z = {}
10
by: Bulba! | last post by:
Hello everyone, I'm reading the rows from a CSV file. csv.DictReader puts those rows into dictionaries. The actual files contain old and new translations of software strings. The dictionary...
210
by: Christoph Zwerschke | last post by:
This is probably a FAQ, but I dare to ask it nevertheless since I haven't found a satisfying answer yet: Why isn't there an "ordered dictionary" class at least in the standard list? Time and again...
2
by: David Pratt | last post by:
Hi. I like working with lists of dictionaries since order is preserved in a list when I want order and the dictionaries make it explicit what I have got inside them. I find this combination very...
5
by: Ladislav Andel | last post by:
Hi, I have a list of dictionaries. e.g. how could I make a new list of dictionaries which would look like: , 'service_domain': 'dp0.example.com'}, {'transports': , 'service_domain':...
9
by: Abandoned | last post by:
Hi.. dict1={1: 4, 3: 5}... and 2 millions element dict2={3: 3, 8: 6}... and 3 millions element I want to combine dict1 and dict2 and i don't want to use FOR because i need to performance. ...
0
by: Jon Bowlas | last post by:
Many thanks for all your reponses, much appreciated. I'll get back to you on which is the best for me. BTW - yes John thats exactly what I wanted. Cheers Jon
1
by: Edwin.Madari | last post by:
by the way, iterating over bar will throw KeyError if that key does not exist in foo. to see that in action, simply set another key in bar after copy.deepcopy stmt in this example.. bar = 0 and...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.