Home > Net >  How can I get each implementation into one list so that I have access to each string
How can I get each implementation into one list so that I have access to each string

Time:07-15

for i in range(0,len(new)):
    m = re.search(r"core-\b(\d )", new[i])
    if (m):
        m_1 = []
        m_1.append(m.group(1))
        print(m_1)

The implementation I have above places each iteration in its own list

CodePudding user response:

The scope of m_1 is within the search done on each element of the list. It should be outside the scope of the loop if you want to store

import re


m_1 = []
for i in range(0,len(new)):
    m = re.search(r"core-\b(\d )", new[i])
    if (m):
        m_1.append(m.group(1))
        print(m_1)

CodePudding user response:

Try using the .extend() list function instead of .append(). Using extend() will mean that your final list m_1 will be one list of elements, while append() results in a list of lists.

  • Related