python - iterating over a list by a certain number -
this question has answer here:
- how split list evenly sized chunks? 52 answers
suppose have following list:
['id-1213fsr53', 'waiting', '54-81-24-40', 'world', 'id-343252sfsg', 'waiting', '54-41-06-143', 'hello'] the list can subdivided tuples in following format:
(id, status, ip-address, name) so, in other check status, need iterate on list 4 element tuples, like:
for s in status: (id, status, ip-address, name) = s # (s 4 element list) # increment s 4, , continue loop how can achieve such behavior in python?
using method of grouping elements groups zip() docs:
data = ['id-1213fsr53', 'waiting', '54-81-24-40', 'world', 'id-343252sfsg', 'waiting', '54-41-06-143', 'hello'] s in zip(*[iter(data)]*4): (id, status, ip_address, name) = s you can perform unpacking in iteration:
for id, status, ip_address, name in zip(*[iter(data)]*4): # here zip() call doing allows type of iteration:
>>> zip(*[iter(data)]*4) [('id-1213fsr53', 'waiting', '54-81-24-40', 'world'), ('id-343252sfsg', 'waiting', '54-41-06-143', 'hello')]
Comments
Post a Comment