Python Append items to dictionary in a loop

Question:

I am trying to append values to a dictionary inside a loop, but somehow it’s only appending one of the values. I recreated the setup using the same numbers I am dynamically getting.

The output from "print(vertex_id_from_shell)" is "{0: [4], 1: [12], 2: [20]}". I need to keep the keys, but add the remaining numbers to the values.

Thanks.

shells = {0: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], 1: [14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27], 2: [28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41]}
uvsID = [0, 1, 3, 2, 2, 3, 5, 4, 4, 5, 7, 6, 6, 7, 9, 8, 1, 10, 11, 3, 12, 0, 2, 13, 14, 15, 16, 17, 17, 16, 18, 19, 19, 18, 20, 21, 21, 20, 22, 23, 15, 24, 25, 16, 26, 14, 17, 27, 28, 29, 30, 31, 31, 30, 32, 33, 33, 32, 34, 35, 35, 34, 36, 37, 29, 38, 39, 30, 40, 28, 31, 41]
vertsID = [0, 1, 3, 2, 2, 3, 5, 4, 4, 5, 7, 6, 6, 7, 1, 0, 1, 7, 5, 3, 6, 0, 2, 4, 8, 9, 11, 10, 10, 11, 13, 12, 12, 13, 15, 14, 14, 15, 9, 8, 9, 15, 13, 11, 14, 8, 10, 12, 16, 17, 19, 18, 18, 19, 21, 20, 20, 21, 23, 22, 22, 23, 17, 16, 17, 23, 21, 19, 22, 16, 18, 20]


vertex_id_from_shell = {}

for shell in shells:
    selection_shell = shells.get(shell)
    
    #print(selection_shell)
    
    for idx, item in enumerate(selection_shell):
        if item in uvsID:
            uv_index =  uvsID.index(item)
            vertex_ids = vertsID[uv_index]
            vertex_id_from_shell[shell] =  [  ( vertex_ids ) ]
    
print(vertex_id_from_shell)
#{0: [4], 1: [12], 2: [20]} 

#desired result
{0: [0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 7, 0, 1, 7, 5, 6, 4], 1: [8, 9, 11, 10, 13, 12, 15, 14, 9, 8, 15, 13, 14, 12], 2: [16, 17, 19, 18, 21, 20, 23, 22, 17, 16, 23, 21, 22, 20]}
Asked By: johancc

||

Answers:

You are setting vertex_id_from_shell[shell] to a new list, containing only one item every time. Instead, you should append to it.
But first, of course that list needs to exist, so you should check and create it if it doesn’t already exist.

shells = {0: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13], 1: [14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27], 2: [28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41]}
uvsID = [0, 1, 3, 2, 2, 3, 5, 4, 4, 5, 7, 6, 6, 7, 9, 8, 1, 10, 11, 3, 12, 0, 2, 13, 14, 15, 16, 17, 17, 16, 18, 19, 19, 18, 20, 21, 21, 20, 22, 23, 15, 24, 25, 16, 26, 14, 17, 27, 28, 29, 30, 31, 31, 30, 32, 33, 33, 32, 34, 35, 35, 34, 36, 37, 29, 38, 39, 30, 40, 28, 31, 41]
vertsID = [0, 1, 3, 2, 2, 3, 5, 4, 4, 5, 7, 6, 6, 7, 1, 0, 1, 7, 5, 3, 6, 0, 2, 4, 8, 9, 11, 10, 10, 11, 13, 12, 12, 13, 15, 14, 14, 15, 9, 8, 9, 15, 13, 11, 14, 8, 10, 12, 16, 17, 19, 18, 18, 19, 21, 20, 20, 21, 23, 22, 22, 23, 17, 16, 17, 23, 21, 19, 22, 16, 18, 20]


vertex_id_from_shell = {}

for shell in shells:
    selection_shell = shells.get(shell)
    
    #print(selection_shell)
    
    for idx, item in enumerate(selection_shell):
        if item in uvsID:
            uv_index =  uvsID.index(item)
            vertex_ids = vertsID[uv_index]

            # if the list does not exist, create it
            if shell not in vertex_id_from_shell:
                vertex_id_from_shell[shell] = []

            # append to list
            vertex_id_from_shell[shell].append(vertex_ids)
    
print(vertex_id_from_shell)
# {0: [0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 7, 5, 6, 4],
#  1: [8, 9, 11, 10, 13, 12, 15, 14, 9, 8, 15, 13, 14, 12],
#  2: [16, 17, 19, 18, 21, 20, 23, 22, 17, 16, 23, 21, 22, 20]}
Answered By: Cagri

Can you be more specifically? What result do you expected to get? You can give an example.

Answered By: 周michael

You’re overwriting vertex_id_from_shell[shell] each time through the loop, not appending to it.

Use collections.defaultdict() to automatically create the dictionary elements with an empty list if necessary, then you can append.

from collections import defaultdict

vertex_id_from_shell = defaultdict(list)

for shell, selection_shell in shells.items():
    for item in selection_shell:
        if item in uvsID:
            uv_index =  uvsID.index(item)
            vertex_ids = vertsID[uv_index]
            vertex_id_from_shell[shell].append(vertex_ids)
Answered By: Barmar
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.