Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8904399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:07:13+00:00 2026-06-15T02:07:13+00:00

In python, I have a dictionary like this… pleio = {‘firstLine’: {‘enf1′: [’54’, ‘set’],

  • 0

In python, I have a dictionary like this…

pleio = {'firstLine': {'enf1': ['54', 'set'], 
                      'enf2': ['48', 'free'], 
                      'enf3': ['34', 'set'], 
                      'enf4': ['12', 'free']}

        'secondLine':{'enf5': ['56','bgb']
                      'enf6': ['67','kiol']
                      'enf7': ['11','dewd']
                      'enf8': ['464','cona']}}

I would like to make paired combinations with no repetition of the elements in the inner dictionary, to end up with a result like this…

{'enf3': ['34', 'set'], 'enf2': ['48', 'free']}
{'enf3': ['34', 'set'], 'enf1': ['54', 'set']}
{'enf3': ['34', 'set'], 'enf4': ['12', 'free']}
{'enf2': ['48', 'free'], 'enf1': ['54', 'set']}
{'enf2': ['48', 'free'], 'enf4': ['12', 'free']}
{'enf1': ['54', 'set'], 'enf4': ['12', 'free']}

I built a function which lets me do it…

import itertools

def pairwise():
    '''
    '''
    leti=[]
    for snp, enfs in pleio.items():        
        for x in itertools.combinations(enfs, 2 ):
            leti.append(x)    
    pleopairs=[]
    for i in leti:
        pipi={}
        for c in i:
            pipi[c]= enfs[c]
        pleopairs.append(pipi)

..but i was wondering if there’s a more efficient way, like another specific function from itertools, or any other source. By the way, I found a function called “pairwise” in the itertools documentation. But I don’t know how to adapt it, if would be possible in my case, or improve my attempt. Any help?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:07:14+00:00Added an answer on June 15, 2026 at 2:07 am

    Your combinations approach was correct, you just need to turn the results of each combination into a dict again:

    import itertools
    
    def pairwise(input):
        for values in input.itervalues():
            for pair in itertools.combinations(values.iteritems(), 2):
                yield dict(pair)
    

    This version is a generator, yielding pairs efficiently, nothing is held in memory any longer than absolutely necessary. If you need a list, just call list() on the generator:

    list(pairwise(pleio))
    

    Output:

    >>> from pprint import pprint
    >>> pprint(list(pairwise(pleio)))
    [{'enf2': ['48', 'free'], 'enf3': ['34', 'set']},
     {'enf1': ['54', 'set'], 'enf3': ['34', 'set']},
     {'enf3': ['34', 'set'], 'enf4': ['12', 'free']},
     {'enf1': ['54', 'set'], 'enf2': ['48', 'free']},
     {'enf2': ['48', 'free'], 'enf4': ['12', 'free']},
     {'enf1': ['54', 'set'], 'enf4': ['12', 'free']}]
    

    You can even combine the whole thing into a one-liner generator:

    from itertools import combinations
    
    for paired in (dict(p) for v in pleio.itervalues() for p in combinations(v.iteritems(), 2)):
        print paired
    

    Which outputs:

    >>> for paired in (dict(p) for v in pleio.itervalues() for p in combinations(v.iteritems(), 2)):
    ...     print paired
    ... 
    {'enf3': ['34', 'set'], 'enf2': ['48', 'free']}
    {'enf3': ['34', 'set'], 'enf1': ['54', 'set']}
    {'enf3': ['34', 'set'], 'enf4': ['12', 'free']}
    {'enf2': ['48', 'free'], 'enf1': ['54', 'set']}
    {'enf2': ['48', 'free'], 'enf4': ['12', 'free']}
    {'enf1': ['54', 'set'], 'enf4': ['12', 'free']}
    

    If you are on Python 3, replace .itervalues() and .iteritems() by .values() and .items() respectively.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a python dictionary of type defaultdict(list) This dictionary is something like this:
I have a python dictionary like this {'OR': [{'AND': [{'column': 'XXX', 'operator': '=', 'value':
I have a dictionary in python like this: x = {country:{city:population}:......} and I want
Possible Duplicate: Why is python ordering my dictionary like so? I have this fully
I have data in a python dictionary that I'd like to store in a
Say I have a string in Python 3.2 like this: '\n' When I print()
I have a python dict like this: my_dict = {'find': ['http://time.com', 'http://find.com'], 'time': ['http://any.com',
if I have a dictionary like this >>> d = {10: 3, 100: 2,
I have a human dictionary file that looks like this in eng.dic (image that
I have followed this How do I JSON serialize a Python dictionary? and this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.