-1

Let's say we have a list: listA = ['stack', 'overflow', '1', '2', '3', '4', '1', '5', '3', '7', '2', '3', 'L', '1', ..., 'a', '23', 'Q', '1']

I want to create a new list such as: new_list = ['1234', '1537', '23L1', ..., 'a23Q1']

So, in this case I want to create a new list using "listA" by removing first two elements and merging all next elements in groups of 4, so the elements: 1, 2, 3, 4; are one element now.

How to approach this in case that I have a very long list to modify. Also, would be nice to know how to approach this problem in case I don't need to create a new list, if all I want is just to modify the list I already have (such as: listA = ['1234', '1537', '23L1', ..., 'a23Q1']). Thanks.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • For removing elements from a list, that should be covered in any basic Python tutorial, which is outside the scope of SO. Here's two methods in the official tutorial: [Lists - "... lists can be indexed and sliced"](https://docs.python.org/3/tutorial/introduction.html#lists) and [The `del` statement](https://docs.python.org/3/tutorial/datastructures.html#the-del-statement) – wjandrea Dec 06 '21 at 21:28

3 Answers3

1

You can create an iterator over that list and then use zip with four identical copies of that iterator to combine each four consecutive elements:

>>> it = iter(listA[2:])
>>> [''.join(x) for x in zip(*[it]*4)]
['1234', '1537', '23L1', 'a23Q1']

itertools.islice allows you to avoid making a temporary copy of that list via slicing:

>>> import itertools
>>> it = itertools.islice(iter(listA), 2, None)
a_guest
  • 34,165
  • 12
  • 64
  • 118
0

If you don't need a new list you could just create one then set the old list to equal it afterwards. You could try this iterative approach:

listA = ['stack', 'overflow', '1', '2', '3', '4', '1', '5', '3', '7', '2', '3', 'L', '1', 'a', '23']
new_list = [""]
count = 0
for item in listA:
    if count % 4 == 0 and count > 0:
        new_list.append("")
    new_list[-1] += item
    count += 1
listA = new_list
print(listA)

Output:

['stackoverflow12', '3415', '3723', 'L1a23']
Erik McKelvey
  • 1,650
  • 1
  • 12
  • 23
0

For your specific question, I would just loop through it. I would do something like this. (Sorry if the format is a bit off this is one of my first answers) This will loop through and combine all complete sets of 4 elements. You could add custom logic if you wanted to keep the end as well. If you don't want to create a new list you can just use "ListA" and ignore the data scrubbing I did by using ListB.

listA = ['stack', 'overflow', '1', '2', '3', '4', '1', '5', '3', '7', '2', '3', 'L', '1', 'a', '23', 'Q', '1']
listB = listA[2:] # remove first two elements
partialList = []
wholeList = []
position = 0
for element in listB:
  if position < 4:
    partialList.append(element)
    position+=1
  else:
    wholeList.append(''.join(partialList))
    position = 0
    partialList = []
    partialList.append(element)
print(wholeList)
Chris
  • 131
  • 4