Dynamically assign values I'm looking to improve the efficiency of my code.  Although my current method works, I feel it can be improved.

if ouroraddrlen == (4,):
        ouropip = struct.unpack("&gt;bbbb", payload[6:10]) # Need to change this to accept ipv6 as well 
        print "Our I.P : ", ouropip
        notheirip = struct.unpack("&gt;b", payload[10])
        print "No. of their I.P's : ", notheirip    
        theiroripv = struct.unpack("&gt;b", payload[11])   
        print "Their I.P version:: ", theiroripv
        theiroraddrlen = struct.unpack("&gt;b", payload[12])
        print "Length of their Ip : ", theiroraddrlen
        theirfirstip = struct.unpack("&gt;bbbb", payload[13:17])
        print "First Ip : ", theirfirstip
        theirsecondip = struct.unpack("&gt;bbbb", payload[18:22])
        print "Second Ip : ", theirsecondip


The output is:




Time :  (1401734263,)
Our onion address : 
Ip version :  (4,)
Ip length :  (4,)
Our I.P :  ( )
No. of their I.P's :  (2,)
Their I.P version::  (4,)
Length of their Ip :  (4,)
First Ip :  ( )
Second Ip :  ( )





I have removed the real IPs, but they are just IPV4 addresses.

However, what I am wondering, is if it is possible to include an if statement after this section of code:

notheirip = struct.unpack("&gt;b", payload[10])
        print "No. of their I.P's : ", notheirip    


where if the notheirip is greater than zero and depending on the length of:

        theiroraddrlen = struct.unpack("&gt;b", payload[12])
        print "Length of their Ip : ", theiroraddrlen


which would be either 4 or 16, then it would set the payload values of the next section.

For example, if notheirip = (2,) and theiroraddrlen = (4,), then I would want it to print out 

theirip = struct.unpack("&gt;b       &lt;&lt; the number of b required so either 4 or 16 and then the range, this will always start at 13 and go up to either 4 or 16 in the future and loop until all the IPs are displayed.
Your question was a little confusing. But I will try and summarize it before I go into my solution:


You have a struct that you are unpacking and during that process you get the number of IPs 'they' have and the length of those IPs (either 4 or 16)
You want to get all of their IPs dynamically based on that length and number of IPs.


Based on my hopefully correct summary above, you can do this simply with a for loop:

num_their_ips = struct.unpack("&gt;b", payload[10])
len_their_ips = struct.unpack("&gt;b", payload[12])

byte_string = '&gt;{}'.format('b'*len_their_ips[0])
their_ips = []
for count in range(num_their_ips[0]):
    start = 13 + (count*len_their_ips[0])
    end = start + len_their_ips[0]

    ip = struct.unpack(byte_string, payload[start:end])
    print 'IP #{}: {}'.format(count+1, ip)

    their_ips.append(ip)




Now a quick note on your naming conventions. Pythonic convention is to use_underscores in variable names. This is especially needed in your code because your variable names are quite hard to distinguish, as was evident in how much 'trouble' I had converting your names below:

our_op_ip, no_their_ip, their_or_ip_v, their_or_addr_len, their_first_ip, their_second_ip


I would also suggest making your names more descriptive. Right now they feel wordy and are a little confusing. Take no_their_ips as an example: does that mean they don't have any IPs, or is it a shortening of 'number'?

Now, I may be being a tad too purposefully dense. However, this is the way people could look at your code; they may not know what some of the acronyms and abbreviations mean. Writing code with descriptive variable names helps improve the readability of your program.