Problem manipulating lists

Hello,

I want to change the content of a list, I’ve figured out how to do it manually, but that requires me to know the exact number of elements in the list before running the code, which I will not be know in the future.

# example input:
list1 = ["11", "22", "33", "44"]
# wanted output:
list2 = ["1122", "3344"]

I’m want to use a for-loop to make it work, but I just can’t wrap my head around how I’d do it.
Pseudocode: For every 2 items, merge those and put them in another list, until all items in the list have been used.

Right now the code looks like this:

short = list()
long = list()
# Normally the elements in long are retrieved from elsewhere.
long = ["00", "11", "22", "33", "44", "55", "66", "77", "88", "99"]
short.append(long[0] + long[1])
short.append(long[2] + long[3])
short.append(long[4] + long[5])
short.append(long[6] + long[7])
short.append(long[8] + long[9])
print(short)
# ['0011', '2233', '4455', '6677', '8899']

The problem arises whenever the long list contains any other number of elements than 10.
The long list will ALWAYS contain an even number of elements. 2, 4, 6, 8, 10 etc.

I’ve talked to my one and only friend that codes and she was unable to help me.
I’ve spent about 2hours trying to figure out how/what to Google, with no luck.
I’ve tried reading the official docs and whatnot, but unable to find a solution.

What I need is really just a nudge in the right direction in my Googling or a tip on what to use to make things work, don’t want/need the exact solution (that would ruin the fun).

So when long_list has N entries, it should create a short_list with half the amount of entries because it merged pos0 with 1, pos2 with 3 etc until N has been reached.

At what point are you stuck exactly?

As a more general tip I’d suggest starting with a simplified loop, doing just part of the job, for example just iterating over every second item in list. Some other approach might going step-by-step how something like this would be done by hand and then adapting it to code.

1 Like

I read your reply, created some pseducode, bashed my head in the wall for 5mins.
Re-wrote the code.
Created 5 different ways of succeding in doing nothing.
But eventually. FINALLY!
The solution is not pretty, but it works.

long = ["00", "11", "22", "33", "44", "55", "66", "77", "88", "99"]

even = list()
uneven = list()
short = list()


for index in range(0, len(long), 2):
    even.append(long[index])

for index in range(1, len(long), 2):
    uneven.append(long[index])

for index in range(len(even)):
    temp_string = even[index] + uneven[index]
    short.append(temp_string)

print(short)
# Output: ['0011', '2233', '4455', '6677', '8899']

THANK YOU <3
OT: lovely nickname “sanity”, because I’m clearly losing mine, happy to have found yours.

2 Likes

good job! now why don’t you try doing it with a single loop? you totally can, just thibk what you do for every couple of elements

1 Like

Hey!
Maybe it’s a bit late but just saw your post…
I’ve came up with this solution:

for i in range(0,len(long)):
if i%2 == 0:
short.append(long[i]+long[i+1])

print(short)

Basically, you check all the even position numbers of the list (0,2,4,6,etc.) and merge them with the next one.

2 Likes

You could even skip the if-condition by using range(0, len(long), 2).

And when feeling fancy stick it all into a list comprehension:
short = [long[i]+long[i+1] for i in range(0,len(long),2)]

1 Like

It seems like you’ve solved the problem, but for the future, it might be useful to remember that formulas:
f(n)=2n
f(n)=2n+1

where n is an integer greater or equal to 0, list respectively, all the even and all the odd numbers.

So in this case you could have used them when describing the index position of a list in a single for lozop

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.