Home > database >  Python, iterate over list of lists and change the original ones
Python, iterate over list of lists and change the original ones

Time:10-30

I have a list of lists of different lengths, containing some strings.

list_01 = ['ba', '4', 'cdf']
list_02 = ['a4a', '56', 'scdf']
...
list_N = ['baaa', 'dgg', '12f']

and I need to split the strings containing multiple characters in single ones, so that I get something like:

list_01 = ['b', 'a', '4', 'c', 'd', 'f']
list_02 = ['a', '4', 'a', '5' '6', 's', 'c','d', 'f']
...
list_N = ['b', 'a', 'a', 'a', 'd', 'g', 'g', '1', '2', 'f']

Now, I tried this piece of code:

    for lst in [list_1, list_2, ... list_N]:
        tmp = []
        for x in lst:
            if len(x)>1:
                tmp.append([char for char in x])
            else:
                tmp.append(x)
        lst = [item for sublist in tmp for item in sublist]

This works within the loop, i.e. each instance of lst is actually what I want, but the changes aren't transferred to list_01...list_N.

I assume there's some point in the code where lst stops being a reference to the current item in the for loop, but it becomes a new object altogether...probably here?

lst = [item for sublist in tmp for item in sublist]

I checked this discussion, and the ways to create a different copy of the list that are mentioned there don't seem to include anything I'm doing in my code. How can I fix this?

CodePudding user response:

You are correct about the new object being created. If you want to keep the same list object you can use clear and extend.:

list_01 = ['ba', '4', 'cdf']
list_02 = ['a4a', '56', 'scdf']
list_N = ['baaa', 'dgg', '12f']

for lst in [list_01, list_02, list_N]:
    tmp = []
    for x in lst:
        if len(x)>1:
            tmp.append([char for char in x])
        else:
            tmp.append(x)
    lst.clear()
    lst.extend([item for sublist in tmp for item in sublist])

for lst in [list_01, list_02, list_N]:
    print(lst)

CodePudding user response:

Here is a simple way. To explode the lists you can join and convert to list. To overwrite you can use slicing:

list_01 = ['ba', '4', 'cdf']
list_02 = ['a4a', '56', 'scdf']
list_N = ['baaa', 'dgg', '12f']

lists = [list_01, list_02, list_N]

for l in lists:
    l[:] = list(''.join(l))
lists

output:

>>> lists
[['b', 'a', '4', 'c', 'd', 'f'],
 ['a', '4', 'a', '5', '6', 's', 'c', 'd', 'f'],
 ['b', 'a', 'a', 'a', 'd', 'g', 'g', '1', '2', 'f']]

>>> list_01
['b', 'a', '4', 'c', 'd', 'f']

CodePudding user response:

There are multiple ways possible to solve the problem.

But the important thing to keep in mind is, we can just try to manipulate the content of the lists and not reassign the value for list.

This means, all the list operations like clear(), join(), append(), extend(), list slicing etc. can be done and the the changes will reflect in the original list.

If we try to reassign a new list as value, it will not reflect in the original list. Example would be like the one you tried in your code. That is assigning a new value to list.

Try this:

from copy import deepcopy

list_01 = ['ba', '4', 'cdf']
list_02 = ['a4a', '56', 'scdf']
list_N = ['baaa', 'dgg', '12f']

for lst in [list_01, list_02, list_N]:
    tmp = deepcopy(lst)
    lst.clear()
    lst.extend([letter for word in tmp for letter in word])

for lst in [list_01, list_02, list_N]:
    print(lst)

CodePudding user response:

Assigning the result to lst[:] will do what you want.

I used a different approach for constructing the target. First we join all strings in a list to get one long string with ''.join(lst). Then we split this string into single characters by calling list.

list_01 = ['ba', '4', 'cdf']
list_02 = ['a4a', '56', 'scdf']
list_N = ['baaa', 'dgg', '12f']

for lst in [list_01, list_02, list_N]:
    lst[:] = list(''.join(lst))

print(list_N)

For list_N you get the output:

['b', 'a', 'a', 'a', 'd', 'g', 'g', '1', '2', 'f']

CodePudding user response:

Use Map function to reduce the time complexity if you have huge volume of list:

list_01 = ['ba', '4', 'cdf']
list_02 = ['a4a', '56', 'scdf']
list_N = ['baaa', 'dgg', '12f']

for list_ in [list_01, list_02,list_N]:
  temp = []
  for item in list_:
    temp = temp   list(map(lambda x: x, item))
  print(temp)
  • Related