pabaso
pabaso

Reputation: 13

How to fix for-loop output in python

I have 3 routers on my test lab environment, I'm trying to reach these routers and send some commands using for-loop and send output in the way I type/list them.

I have 3 1841 Cisco routers and running 12.4 IOS:

    multi_ip_addr = ['192.168.1.1', '192.168.2.1', '192.168.3.1']
    for ips in multi_ip_addr:
        pass
    len_ip_addr = len(multi_ip_addr)
    for len_ip in range(len_ip_addr):
        device = {'ip': ips, 
        'username': 'admin', 
        'password': 'password'}
        print(device)

My expected result is:

{'ip': '192.168.1.1', 'username': 'admin', 'password': 'password'} {'ip': '192.168.2.1', 'username': 'admin', 'password': 'password'} {'ip': '192.168.3.1', 'username': 'admin', 'password': 'password'}

however I am getting this output:

{'ip': '192.168.3.1', 'username': 'admin', 'password': 'password'} {'ip': '192.168.3.1', 'username': 'admin', 'password': 'password'} {'ip': '192.168.3.1', 'username': 'admin', 'password': 'password'}

Upvotes: 1

Views: 61

Answers (4)

Rob Truxal
Rob Truxal

Reputation: 6408

Python determines context with indentation, so if you want to use your ips variable for each entry, you need to do it within the for ips in multi_ip_addr context (where you currently only have pass.) I might also change the name of ips to ip if I were you, just to make things clearer.

Here's how I would rewrite your loop:

template_dict = {'ip' : None, 'username' : 'admin', 'password' : 'password'}
multi_ip_addr = ['192.168.1.1', '192.168.2.1', '192.168.3.1']

for ip in multi_ip_addr:
    print(template_dict.update({'ip' : ip})

Comparison with JS:

Using range(len(multi_ip_addr)) will give you a set of index values, but this is unnecessary for what you are trying to accomplish. If you're familiar with JavaScript: the default for loop behavior in python is like the using the for...of pattern:

EX:

for (let i of <some_iterable>) {
    console.log(i);
}

would be equivalent to:

for i in <some_iterable>:
    print(i)

Upvotes: 0

N Chauhan
N Chauhan

Reputation: 3515

You need to get the item out of the list of IPs. Use the for loop for that, not a range:

for ip in multi_ip_addr:
    device = {
        'ip': ip,
        'username': 'admin',
        'password': 'password'
    }
    print(device)

The first for loop will overwrite the value of ips on each loop, so really by the end of the loop, ips equals the last value of multi_ip_addr.

Upvotes: 2

John Gordon
John Gordon

Reputation: 33275

for ips in multi_ip_addr:
    pass

As this loop executes, ips takes on the value of each item in multi_ip_addr. When the loop ends, ips retains the value of the last item.

In the next loop, you never change the value of ips, so it has the same value every time.

Upvotes: 1

buran
buran

Reputation: 14218

multi_ip_addr = ['192.168.1.1', '192.168.2.1', '192.168.3.1']
for ips in multi_ip_addr:
    device = {'ip':ips, 'username':'admin', 'password':'password'}
    print(device)

Upvotes: 2

Related Questions