Enigmatr
Enigmatr

Reputation: 15

Iterate through a dictionary inside a for loop

I'm trying to print the values of a dictionary inside a for loop, currently this is what I'm getting:

    Site 1
    {'Port': '1/0/2', 'Link-Status': 'Up', 'Vlans': '81,1001,1101,1301,1303'}
    {'Port': '1/0/4', 'Link-Status': 'Down', 'Vlans': '1007'}
    {'Port': '1/0/11', 'Link-Status': 'Up', 'Vlans': '30,31,81,82'}
    {'Port': '1/0/13', 'Link-Status': 'Up', 'Vlans': '30,31,81,82,1001,1101'}
    {'Port': '1/0/14', 'Link-Status': 'Up', 'Vlans': '1007'}
    Site 1
    {'Port': '1/0/2', 'Link-Status': 'Up', 'Vlans': '82,1301,2001,2101'}
    {'Port': '1/0/4', 'Link-Status': 'Down', 'Vlans': '2007'}
    {'Port': '1/0/11', 'Link-Status': 'Up', 'Vlans': '30,31,81,82'}
    {'Port': '1/0/13', 'Link-Status': 'Up', 'Vlans': '30,31,81,82,1301,2001,2101'}
    {'Port': '1/0/14', 'Link-Status': 'Up', 'Vlans': '2007'}
    Site 1
    {'Port': '1/0/2', 'Link-Status': 'Up', 'Vlans': '1006,1102'}
    {'Port': '1/0/4', 'Link-Status': 'Down', 'Vlans': '1007'}
    {'Port': '1/0/11', 'Link-Status': 'Up', 'Vlans': '20,21'}
    {'Port': '1/0/13', 'Link-Status': 'Up', 'Vlans': '20,21,1006,1102'}
    {'Port': '1/0/14', 'Link-Status': 'Up', 'Vlans': '1007'}
    Site 1
    {'Port': '1/0/2', 'Link-Status': 'Down', 'Vlans': '2006,2102'}
    {'Port': '1/0/4', 'Link-Status': 'Down', 'Vlans': '2007'}
    {'Port': '1/0/11', 'Link-Status': 'Up', 'Vlans': '20,21'}
    {'Port': '1/0/13', 'Link-Status': 'Up', 'Vlans': '20,21,2006,2102'}
    {'Port': '1/0/14', 'Link-Status': 'Up', 'Vlans': '2007'}

I have tried indexing the values inside the for loop, but I haven't had luck with this:

    def main():
        input_site = new_site.keys()
        print(new_site.values())
        for element in input_site:
            i=0
            print(list(new_site.values())[i])
            i+=1
            processInput(element,type)

This is the content of my dictionary:

    print(new_site.values())
    dict_values(['Site 1', 'Site 3', 'Site 7', 'Site 9'])

This is what I'm aiming for:

    Site 1
    {'Port': '1/0/2', 'Link-Status': 'Up', 'Vlans': '81,1001,1101,1301,1303'}
    {'Port': '1/0/4', 'Link-Status': 'Down', 'Vlans': '1007'}
    {'Port': '1/0/11', 'Link-Status': 'Up', 'Vlans': '30,31,81,82'}
    {'Port': '1/0/13', 'Link-Status': 'Up', 'Vlans': '30,31,81,82,1001,1101'}
    {'Port': '1/0/14', 'Link-Status': 'Up', 'Vlans': '1007'}
    Site 3
    {'Port': '1/0/2', 'Link-Status': 'Up', 'Vlans': '82,1301,2001,2101'}
    {'Port': '1/0/4', 'Link-Status': 'Down', 'Vlans': '2007'}
    {'Port': '1/0/11', 'Link-Status': 'Up', 'Vlans': '30,31,81,82'}
    {'Port': '1/0/13', 'Link-Status': 'Up', 'Vlans': '30,31,81,82,1301,2001,2101'}
    {'Port': '1/0/14', 'Link-Status': 'Up', 'Vlans': '2007'}
    Site 7
    {'Port': '1/0/2', 'Link-Status': 'Up', 'Vlans': '1006,1102'}
    {'Port': '1/0/4', 'Link-Status': 'Down', 'Vlans': '1007'}
    {'Port': '1/0/11', 'Link-Status': 'Up', 'Vlans': '20,21'}
    {'Port': '1/0/13', 'Link-Status': 'Up', 'Vlans': '20,21,1006,1102'}
    {'Port': '1/0/14', 'Link-Status': 'Up', 'Vlans': '1007'}
    Site 9
    {'Port': '1/0/2', 'Link-Status': 'Down', 'Vlans': '2006,2102'}
    {'Port': '1/0/4', 'Link-Status': 'Down', 'Vlans': '2007'}
    {'Port': '1/0/11', 'Link-Status': 'Up', 'Vlans': '20,21'}
    {'Port': '1/0/13', 'Link-Status': 'Up', 'Vlans': '20,21,2006,2102'}
    {'Port': '1/0/14', 'Link-Status': 'Up', 'Vlans': '2007'}

Any suggestions?

Upvotes: 1

Views: 164

Answers (2)

leewz
leewz

Reputation: 3346

Other than the i=0 problem, there are some major issues with your for-loop:

for element in input_site:
    print(list(new_site.values())[i])
    i+=1
    processInput(element,type)
  • Every time you iterate, you copy the ENTIRE list of values, just to access a single element.
  • element is a key in the new_site dict. There are ways to ask for the value associated with a key. Go over the tutorial and the documentation for dict.
  • When iterating over a dict, if you want both keys and values, you should use .items(), not .keys(). Sample code:

    for key, value in d.items():
        ...
    
  • When you pass type to processInput, you are passing the function named type. type is a function that takes in an object and tells you what type it has. However, when you pass type, you are just passing the function itself. Is that really what you want? You need to call the function on an object to get its type.

I recommend reviewing the material, or finding a tutor to explain to you what each part of your code does. Prepare some questions beforehand.

Upvotes: 0

abc
abc

Reputation: 11929

Inside your for-loop you have the following lines

for element in input_site:
    i=0 # <---
    print(list(new_site.values())[i])
    i+=1

At each iteration you assign i to be 0, then print the i-th element of list(new_site.values()) and then increase its value by one.
The same will happen for the next iterations.
So, to fix it, i should be moved outside the loop.

Upvotes: 2

Related Questions