Multiple list comparison Python I have some code that has three lists, and it then checks the index of the second list against the index of the first as long as the first list has six elements. The code will then append to the third list if part of a string matches in the index of the first list. If the string does not match, it will append a message. I am hoping to find a better, more Pythonic way of writing my algorithm. Here is my code:
L1 = [&quot;first = 1st&quot;,&quot;second = 2nd&quot;,&quot;third = 3rd&quot;,&quot;fourth = 4th&quot;,&quot;sixth = 6th&quot;,
  &quot;first = A&quot;,&quot;second = B&quot;,&quot;third = C&quot;,&quot;fifth = E&quot;,&quot;sixth = F&quot;,
  &quot;second = W&quot;,&quot;third = X&quot;,&quot;fourth = Y&quot;,&quot;fifth = Z&quot;,&quot;sixth = AA&quot;,&quot;first = BB&quot;]
L2 = [&quot;first&quot;,&quot;second&quot;,&quot;third&quot;,&quot;fourth&quot;,&quot;fifth&quot;,&quot;sixth&quot;]
L3 = []

#Used in case a list has less than six elements
if len(L1) % 6 != 0:
    L1.append(&quot;Missing_Data&quot;)

c = 0
for i in L1:
    cont = True
    while cont:
        if L2[c] in i:
            L3.append(i.split(&quot;= &quot;)[-1])
            c += 1
            if c &lt; len(L2):
                cont = False
            else:
                c = 0
                cont = False
        else:
            L3.append(&quot;Missing_Data&quot;)
            c += 1
            if c &lt; len(L2):
                continue
            else:
                c = 0
                break

This code works for what I want it to do but I think its too long. Any help in making this code more Pythonic would be greatly appreciated. Thanks in advance.
My review of your current code is easy to summarize: It's too darn complicated and
makes my head hurt. But don't feel bad, because you're in great company. First
attempts are often like that – even for people who have been doing this
for a long time.
What, specifically, makes it hard to understand? Algorithmic complexity: nested loops
and conditionals, breaks, managing list indexes, and so forth. Whenever you perceive that type of complexity, it often helps to consider whether a more powerful data structure
would simplify the situation (I'm using &quot;data structure&quot; in a very broad sense).
In the rewrite offered below, we will use
a special iterable that allows us to peek at the next value without actually
consuming it every time. That one change drastically simplifies the bookkeeping
inside the main loop, and it also simplifies how we append the needed
remainder of missing values after we exit the loop.
# This is a third party library that is worth knowing about.
from more_itertools import peekable

# Your data, aligned so we can see what is going on.
# When you ask a question, it's a good idea to help your helpers.
xs = [
    'first = 1st', 'second = 2nd', 'third = 3rd', 'fourth = 4th',              'sixth = 6th',
    'first = A',   'second = B',   'third = C',                   'fifth = E', 'sixth = F',
                   'second = W',   'third = X',   'fourth = Y',   'fifth = Z', 'sixth = AA',
    'first = BB',
]

ys = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth']

# Python has a builtin concept for missing data. Use it if you can.
# If you cannot, define a constant.
MISSING = None

# The results we want.
# When you ask a question, it's a good idea to provide this.
EXPECTED = [
    '1st',     '2nd',     '3rd',     '4th',     MISSING,   '6th',
    'A',       'B',       'C',       MISSING,   'E',       'F',
    MISSING,   'W',       'X',       'Y',       'Z',       'AA',
    'BB',      MISSING,   MISSING,   MISSING,   MISSING,   MISSING
]

# We will use a peekable iterable for both the Xs and the Ys.
xit = peekable(xs)
yit = None
results = []

# Process all Xs to build the results.
# A Y is consumed each time, and we get a fresh Ys iterable as needed.
# We consume an X only when current X and Y agree.
while xit:
    yit = yit or peekable(ys)
    x = xit.peek()
    y = next(yit)
    val = next(xit).split('= ')[-1] if y in x else MISSING
    results.append(val)

# The results should always contain a full cycle of Ys.
results.extend(MISSING for _ in yit)

# Check.
print(results == EXPECTED)