python - iterating over a list by a certain number -


this question has answer here:

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

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -