0

In python, I'd like to make a pairwise iteration through a list of tuples (lot) element. My lot is shown below:

lot = [('490001', 'A-ARM1'),
       ('490001', 'A-ARM2')]

I'd like to create one iteration variable that loops through the second element of every tuple, which will match:

iter: A-, AR, M1    (cycle 1)
iter: A-, AR, M2    (cycle 2)

On stack I found this (Iterate over a string 2 (or n) characters at a time in Python).

Unfortunately, I couldn't get this to work in my example. Could someone give me a push in the right direction?

Jeromekem
  • 85
  • 9
  • 1
    Can you clarify this a little bit? What do you want your loop(s) to look like? Are your iterators over a single element from the `lot` list, or each one in turn? Are nested loops acceptable? For Iter1 are you expecting: 49, 00, 01, (end first element) 49, 00, 01 (end second one) – bjmc Mar 21 '22 at 14:17
  • Thanks for the response bjmc, I adjusted the question for you to make it clearer. Nested loops are acceptable. Hope you can help! – Jeromekem Mar 21 '22 at 14:28

1 Answers1

1

If nested loops are acceptable (per your comment), then I think you can use the grouper recipe from more-itertools (or copy-paste that function in your codebase):

from more_itertools import grouper


lot = [('490001', 'A-ARM1'),
       ('490001', 'A-ARM2')]

# Tuple-unpacking w/underscore is a convention to indicate we don't
# care about the first element, only the second one
for _, elem in lot: 
   for i, j in grouper(elem, 2):
       print(i + j)

# Output
# A-
# AR
# M1
# A-
# AR
# M2
bjmc
  • 2,970
  • 2
  • 32
  • 46