Home > Net >  Python looping list and append value to variable
Python looping list and append value to variable

Time:07-07

I have a .csv file with IPs which I converted into a list with Python:

def ip_list():
    iplist = []
    with open("/path/to/file") as csvfile:
        csvlist = csv.reader(csvfile)
        for lists in csvlist:
            for item in lists:
                iplist.append(item)
    return iplist

ip = ip_list()

print(ip)

>>> ["192.168.1.1", "192.168.1.2", ...]

Now I want to have every value in the list and append them to a given parameter each time.

Function for context:

def gencontent(ip, value1, value2, time):
    content = [
            {
                "example": {
                    "ipadress": ip
                    }
            }
        ]
    return content

ip = ["192.168.1.1", "192.168.1.2", "192.168.1.3"]

content = getcontent(ip[0-...], value1, value2, time)

I want loop content with each value in ip:

#Example list for reproduction
ip = ["192.168.1.1", "192.168.1.2", "192.168.1.3"]

content = getcontent(ip[0-...], ...)

I do not want:

#Example list for reproduction
ip = ["192.168.1.1", "192.168.1.2", "192.168.1.3"]

content1 = getcontent(ip[0], ...)
content2 = getcontent(ip[1], ...)
...

I want to loop content basically each time with a new ip value.

Thanks!

CodePudding user response:

I don't know what the getcontent() function does, but why not loop through the items in your list using a list comprehension?

content = [getcontent(x) for x in ip]

CodePudding user response:

If you simply want to index them, maybe you could convert to a tuple and use enumerate. For example:


ip = ["192.168.1.1", "192.168.1.2", "192.168.1.3"]

indexed_ip = enumerate(tuple(ip))
print(list(indexed_ip))
# OUTPUT: 
# [(0, '192.168.1.1'), (1, '192.168.1.2'), (2, '192.168.1.3')]

Or if you want the index to start at 1, instead of 0:


ip = ["192.168.1.1", "192.168.1.2", "192.168.1.3"]

indexed_ip = enumerate(tuple(ip), 1)
print(list(indexed_ip))
# OUTPUT: 
# [(1, '192.168.1.1'), (2, '192.168.1.2'), (3, '192.168.1.3')]


Alternatively, maybe a dictionary work for you in this situation.

Here’s an example using dictionary comprehension:


ip_dict = { ip.index(ip_item): ip_item for ip_item in ip}
print(ip_dict)
# OUTPUT:
# {0: '192.168.1.1', 1: '192.168.1.2', 2: '192.168.1.3'}

You can name the keys for the dictionary, whatever you’d like. if you’re sent on content0, content1, etc, you could change the key value in the dict comprehension to something like f’content{str(ip.index(ip_item))}’. Then you could get the value from the ip_dict using ip_dict['content1'] and etc.

CodePudding user response:

can you be more specific about content = getcontent(ip[0-...])?

i don't know whether i get you. maybe something like this?

ip = ["192.168.1.1", "192.168.1.2", "192.168.1.3"]
def getip(li):
    for item in li:
        yield(item)

ipgetter = getip(ip)


content = getcontent(next(ipgetter), value1, value2, time) # getcontent got "192.168.1.1"
content = getcontent(next(ipgetter), value1, value2, time) # getcontent got "192.168.1.2"

if loop is in an end, an StopIteration Exception will being raised

  • Related