Home > OS >  How does Python iterate through a list using "for loop"?
How does Python iterate through a list using "for loop"?

Time:04-16

I have this code, and I don't understand why the "print" statement happens to print all of the "item" varaibles as it goes through the for loop. The trainer (an online course) appears to be happy with the output but I don't understand it.

My understanding from C (I understand Python is a different language), is that the "item" variable's value at the end of the loop is when "intf_type = "VLAN"" and the print statement should only print "Interface type of item VLAN100 is VLAN".

Why is the code printing all available list?

Isn't print sitting at the end of the "for loop" and hence it should only print the last executed value of "item" which is VLAN?

Thank you.

Here is the code:

interfaces = [ "GigabitEthernet1/1", "Loopback0", "GigabitEthernet1/2 ", "TenGigabitEthernet1/1", "VLAN100"]

for item in interfaces:
    if item.capitalize().startswith("Gigabit"):
        int_type = "GigabitEthernet"

    elif item.capitalize().startswith("Loop"):
        int_type = "Loopback"

    elif item.capitalize().startswith("Ten"):
        int_type = "TenGigabitEthernet"

    elif item.upper().startswith("VLAN"):
        int_type = "VLAN"

    else:
        int_type = "Unknown"

    print(f"Interface type of item {item} is {int_type}")

Here is the output:

Interface type of item GigabitEthernet1/1 is GigabitEthernet
Interface type of item Loopback0 is Loopback
Interface type of item GigabitEthernet1/2  is GigabitEthernet
Interface type of item TenGigabitEthernet1/1 is TenGigabitEthernet
Interface type of item VLAN100 is VLAN

CodePudding user response:

Your print statement is inside for loop, Try this

interfaces = [ "GigabitEthernet1/1", "Loopback0", "GigabitEthernet1/2 ", "TenGigabitEthernet1/1", "VLAN100"]

for item in interfaces:
    if item.capitalize().startswith("Gigabit"):
        int_type = "GigabitEthernet"

    elif item.capitalize().startswith("Loop"):
        int_type = "Loopback"

    elif item.capitalize().startswith("Ten"):
        int_type = "TenGigabitEthernet"

    elif item.upper().startswith("VLAN"):
        int_type = "VLAN"

    else:
        int_type = "Unknown"

print(f"Interface type of item {item} is {int_type}")

CodePudding user response:

It checks each item in a list one by one and printing the result as expected. You can put an print(item) statement as following to see the result.

interfaces = [ "GigabitEthernet1/1", "Loopback0", "GigabitEthernet1/2 ", "TenGigabitEthernet1/1", "VLAN100"]

for item in interfaces:
    print('----------') # <<<<<<<<<<<<<<<<<<<<<
    print(item) # <<<<<<<<<<<<<<<<<<<<<<<<<<
    if item.capitalize().startswith("Gigabit"):
        int_type = "GigabitEthernet"

    elif item.capitalize().startswith("Loop"):
        int_type = "Loopback"

    elif item.capitalize().startswith("Ten"):
        int_type = "TenGigabitEthernet"

    elif item.upper().startswith("VLAN"):
        int_type = "VLAN"

    else:
        int_type = "Unknown"

    print(f"Interface type of item {item} is {int_type}")

The result is going to be:

enter image description here

So the int_type keep changing for each element of the interfaces list.

  • Related