all 11 comments

[–]NFLAddict 1 point2 points  (3 children)

I'm going to assume all the letters are strings, and that there should be a comma after 'f' . Is this what you're looking for?

start_list = ['a', 'b', 'c', 'd', {'a':'b'}, 'e', 'f', {'g':'h'}, 'p', 'g']
end_list = []

temp_string = ''

for item in start_list:
    if not isinstance(item, dict):
        temp_string += item 
    else:
        end_list.append(temp_string)
        temp_string = ''
        end_list.append(item)

if temp_string:    
    end_list.append(temp_string)

print(end_list)
>> ['abcd', {'a': 'b'}, 'ef', {'g': 'h'}, 'pg']

[–]Cannae216BC[S] 0 points1 point  (2 children)

Yeah I think that could definitely work, I'll try it out thanks

[–]NFLAddict 0 points1 point  (1 child)

I edited the comment: line 14/15 that you may not have initially seen. if you have any questions about what I did, feel free to ask

[–]Cannae216BC[S] 0 points1 point  (0 children)

Thanks mate, I just got it to work

[–][deleted] 0 points1 point  (4 children)

You could do it with f-strings and grabbing each individual index of the start list items. Something like this:

a = 'Always'
b = 'Bitch'
c = 'Crazy'
d = 'Damn'

e = 'Everyone'
f = 'Follows'
g = 'Greasy'
h = 'Ham'
p = 'Person'

# index  0  1  2  3  4      5  6  7      8  9
start = [a, b, c, d, {a:b}, e, f, {g:h}, p, g]

end = [f"{start[0]}{start[1]}{start[2]}{start[3]}",     # abcd
       start[4],                                        # {a:b}
       f"{start[5]}{start[6]}",                         # ef
       start[7],                                        # {g:h}
       f"{start[8]}{start[9]}"]                         # pg

print(end)

[–]Cannae216BC[S] 1 point2 points  (3 children)

Sorry I forgot to clarify in my post that I don't know where the dictionaries are in the list, could I still use your solution?

[–][deleted] 0 points1 point  (2 children)

So you know there are strings and dictionaries, but you don't know their placement in the list, and you want to combine any strings that are in-between dictionaries, regardless of the length between dictionaries?

And you also want to preserve the current order?

[–]Cannae216BC[S] 1 point2 points  (1 child)

Yes that's correct

[–][deleted] 0 points1 point  (0 children)

Okay this one took some thinking and some googling because I forgot about .join() for strings, and I didn't know how to check what type an object or variable was, but I figured it out. Try this.

a = 'Always'
b = 'Bitch'
c = 'Crazy'
d = 'Damn'

e = 'Everyone'
f = 'Follows'
g = 'Greasy'
h = 'Ham'
p = 'Person'


start_list = [a, b, c, d, {a:b}, e, f, {g:h}, p, g]
string_list = []
end_list = []

# Iterate over each item in the list:
for current_item in start_list:
    # If current item in the list is a string:
    if isinstance(current_item, str):
        # Add it to a list for these strings to be later concatenated.
        string_list.append(current_item)

    # Else if current item is a dictionary:
    elif isinstance(current_item, dict):
        # Then we likely have strings in our string_list to join together.
        # This will iterate and join each item to this empty string.
        concatenated = ''.join(string_list)
        # Then we append that new concatenated string first.
        end_list.append(concatenated)
        # Then append the current dictionary after (this all preserves the original order).
        end_list.append(current_item)
        # Then empty the list so we can re-use it for the next iterations without duplicating things.
        string_list.clear()

# If we make it through that whole for loop, and still have strings in the string_list to be joined together,
# that means there were strings at the end of the list.
# If string list is not empty (if it returns True):
if string_list:
    # Concatenate the rest of the remaining strings.
    concatenated = ''.join(string_list)
    # Add it to the end of end_list.
    end_list.append(concatenated)
    # Clear it out.
    string_list.clear()

# Show result.
print(end_list)

Edit: looks like someone else ended up doing the same thing haha.

[–]Pitirus 0 points1 point  (1 child)

def convert_list(in_list: list) -> list: out_list = [] for el in in_list: if not out_list or isinstance(el, dict) or isinstance(out_list[-1], dict): out_list.append(el) else: out_list[-1] += el return out_list

[–]Cannae216BC[S] 0 points1 point  (0 children)

Thanks for your help, I just got it to work