Will
Will

Reputation: 3

Creating Dictionary in Python via Multiple for Loops

I know the syntax in the following is off, but I'm having trouble figuring out how this should be written. What I'm trying to do is take the list "list" and create a dictionary where the keys are the combination of each words with each other word that isn't itself and the value for every key is 0. Here's my broken code:

lst = ('human', 'loud', 'big')
for words in lst:
    first = words
    for words in lst:
        if words != first:
            scores = {'%s + %s': 0, % (first, words)}

Dictionary would look like this:

scores = {'human + loud': 0, 'human + big': 0, 'loud + big': 0, 'loud + human': 0, 'big + loud': 0, 'big + human': 0}

Any help is greatly appreciated!

Edited: To change list type to lst.

Upvotes: 0

Views: 103

Answers (6)

Burger King
Burger King

Reputation: 2965

I have a more compact function.

import itertools
scores = {}
for a,b in itertools.permutations(('human', 'loud', 'big'), 2):
    scores["{0} + {1}".format(a,b)] = 0
print scores

Upvotes: 1

merlin2011
merlin2011

Reputation: 75649

You have a syntax issue with the dictionary assignment line, but the more important issue is that you're re-assigning the dictionary every time.

Instead, create the dictionary at the start and then just add to it.

Finally list is not a good variable name because it collides with the name of the type.

mylist = ('human', 'loud', 'big')
scores = {}
for w in mylist:
    for v in mylist:
        if w != v:
            scores['%s + %s'  % (w, v)] = 0

print scores

Upvotes: 1

Craig Burgler
Craig Burgler

Reputation: 1779

You had the right idea - here's your basic logic with the syntax fixed. The two primary changes are: 1) you need to name the iterator variable in your inner loop something different than the iterator variable in your outer loop, otherwise the scopes will conflict, and 2) to add things to dictionaries, use the [key] index notation rather than trying to create a whole new dictionary with curly brace notation.

list = ['human', 'loud', 'big']
scores = {}
for i in range(len(list)):
    for j in range(len(list)): 
        if i != j:
            key = list[i] + " + " + list[j]
            scores[key] = 0

Upvotes: 0

m4p85r
m4p85r

Reputation: 402

Here's my snippet that worked. I recommend creating the dictionary and then just adding to it.

    scores = {}

    lst = ('human', 'loud', 'big')

    for words in list:
        first = words
        for words in list:
            if words != first:
                scores.update({first + " + " +  words: 0}) 

    print scores

I'd recommend changing some of your variable names to differentiate between the words being used in each nested list. As then you can do away with the 'first' variable entirely.

    scores = {}

    lst = ('human', 'loud', 'big')

    for word1 in list:
        for word2 in list:
            if word2 != word1:
                scores.update({word1 + " + " +  word2: 0}) 

    print scores

Upvotes: 0

chepner
chepner

Reputation: 532508

Use itertools.combinations:

from itertools import combinations as comb
l = ['human', 'loud', 'big']
scores = dict((" + ".join(pair), 0)
              for ordered_pair in comb(l, 2)
                for pair in (ordered_pair, reversed(ordered_pair)))

Upvotes: 0

user1823
user1823

Reputation: 1111

Try the following:

lst = ('human', 'loud', 'big')
scores = {}
for f in lst:
    for s in lst:
        if f != s:
            scores['{0} + {1}'.format(f, s)] = 0

print scores

As such:

>>> lst = ('human', 'loud', 'big')
>>> scores = {}
>>> for f in lst:
...     for s in lst:
...         if f != s:
...             scores['{0} + {1}'.format(f, s)] = 0
... 
>>> print scores
{'human + big': 0, 'big + loud': 0, 'big + human': 0, 'human + loud': 0, 'loud + big': 0, 'loud + human': 0}
>>> 

Upvotes: 1

Related Questions