Creating a filtered list by comparing one list to another other list - Python I have a list and I need to get certain elements out of it. The way I did it was to compare the second list to the first list and then append to a third list. Here is my code:
l1 = [&quot;0 = 0&quot;,&quot;1 = 1&quot;,&quot;2 = 2&quot;, &quot;And&quot;, 
      &quot;Something&quot;, &quot;Wicked&quot;, &quot;This&quot;, &quot;Way&quot;, &quot;Comes&quot;,
      f&quot;zero.0 = 0&quot;, f&quot;one.0 = 1&quot;, f&quot;two.0 = 2&quot;,
      f&quot;zero.1 = 0&quot;, f&quot;one.1 = 1&quot;, f&quot;two.1 = 2&quot;,
      f&quot;zero.2 = 0&quot;, f&quot;one.2 = 1&quot;, f&quot;two.2 = 2&quot;,
      &quot;purple&quot;, &quot;monkey&quot;, &quot;dishwasher&quot;]
l3 = []
for element in l1:
   for i in range(3):
       l2 = [f&quot;zero.{i} = 0&quot;, f&quot;one.{i} = 1&quot;, f&quot;two.{i} = 2&quot;]
       for j in l2:
           if j in element:
               l3.append(element)

The results of this nested for loop is:
['zero.0 = 0', 'one.0 = 1', 'two.0 = 2', 'zero.1 = 0', 'one.1 = 1', 'two.1 = 2', 'zero.2 = 0', 'one.2 = 1', 'two.2 = 2']

The result is exactly what I want but I'm looking for a more Pythonic way of writing this. Any help would be greatly appreciated.
Unnecessary format strings
Unless you intend to format a string right then and there, you don't need an f before it.
Regex
This can be reduced to one line using regex:
import re

l3 = [item for item in l1 if re.match(r&quot;\w{3,5}[.]\d\s[=]\s\d{1,}&quot;, item)]

Explanation

\w{3,5} matches any word character (equal to [a-zA-Z0-9_])

{3,5} Quantifier — Matches between 3 and 5 times, as many times as possible, giving back as needed (greedy)


Match a single character present in the list below [.]

. matches the character . literally (case sensitive)

\d matches a digit (equal to [0-9])

\s matches any whitespace character (equal to [\r\n\t\f\v ])



Match a single character present in the list below [=]

= matches the character = literally (case sensitive)

\s matches any whitespace character (equal to [\r\n\t\f\v ])



\d{1,} matches a digit (equal to [0-9])

{1,} Quantifier — Matches between one and unlimited times, as many times as possible, giving back as needed (greedy)