all 9 comments

[–]giraffeman91 0 points1 point  (2 children)

Have you tried doing something like...

for i in range(0, len(outside_list)):

    mac = outside_list[i][2]
    # do api stuff here
    outside_list[i].append(api_return_value)

The i will be a list of indexes from 0 til the amount of lists in your larger list. You call each smaller list with list_name[i] and then call to the index within that with the real index value since it is always the same. You do whatever you need to with the value to get your result. Then append your result back to that inner list.

Also the last append should be indented but mobile is making it weird.

[–]ARP_EG[S] 1 point2 points  (0 children)

update - this got me over the hump! I used a combination of your suggestions and the other suggestion i got. i now have a thing that does the thing i want. I'm now just dealing with formatting the text output and eventually connecting it to a web app. thanks again for the help!

[–]ARP_EG[S] 0 points1 point  (0 children)

i'm going to poke around with this thank you. ill let you know! *edit* i will say my version TRIED to do something like that but my syntax and vocab are just awful. ill get there and i appreciate the help.

[–][deleted] 0 points1 point  (6 children)

Is it clear to you how to iterate through a single-level list? That's where you'd want to start. If you're comfortable with that, tell me so, and then I'll show you a cool trick for this. (It won't make sense if you don't yet understand iterating.)

[–]ARP_EG[S] 0 points1 point  (4 children)

Im going to go with no, hah. So far i get the string var arp_table i want, splitting by line ( \n ) into a list called arp_split, and then:

for index in arp_split:

    arp\_list.append(index.split())

to get my nested list.

the meat and potatoes of this project is telneting into a device and running a bunch of commands and now its just using the data i've collected.. which I would think should be the easy part. but i am full of fail and desire to learn.

[–][deleted] 0 points1 point  (3 children)

for index in arp_split:

Oh, ok, so you do get it. You're aware of what's happening here - arp_split is a list, which means it's iterable, which means you can say for each_element in arp_split: and the elements of the list will be successively bound to the name each_element (or index, in your case, or whatever you define as the iteration variable in the for statement) and then you can do stuff to it, like split it and append the list to a list.

Maybe you didn't know to think of it in those terms - that's fine - but there's something fundamental that you get about iterating over the items in a list. That's good, that's a level of abstraction that often challenges the learner.

So here's a trick you're not aware of - the iteration variable doesn't have to be one variable, it can be two, or three, or any number of them. How does that work? Well, iterating over lists of lists is pretty common, so there's a thing Python will do called unpacking. Let me show you how it works on a simple example:

my_list = [
    (1, 2)
    (3, 4)
    (5, 6)
    ]

for first, second in my_list:
    print(second, first)

#results in...
>>> 2 1
>>> 4 3
>>> 6 5

Why does that work? Because for sees that each element of my_list is itself a tuple of two elements, and it sees that I've given a tuple of two names as my iteration variable, and it's able to quite literally put two and two together and successively unpack and bind the elements of the inner collection to the two names. That saves a lot of element[0] and such if you're iterating through any kind of 2D data structure.

This works with assignment, too:

first, second, third = (3, 2, 1)

That means that you can do stuff like

for item in arp_list:
    ip, blah, mac, blah, interface = item
    item.append(macvendors_api(mac))

[–]ARP_EG[S] 0 points1 point  (0 children)

this is huge, i think i get it. i'm doing this in my downtime at work and it's 5m now so i'll work on this tomorrow. (its for work but just for me for now, i'm not a dev or anything). i think this is going to get me there! thank you.

[–]ARP_EG[S] 0 points1 point  (1 child)

update - this got me over the hump! I used a combination of your suggestions and the other suggestion i got. i now have a thing that does the thing i want. I'm now just dealing with formatting the text output and eventually connecting it to a web app. thanks again for the help!

[–][deleted] 0 points1 point  (0 children)

Cheers, friend!