how to concat on element to other in same list based on condition

how to concatenate items in list
concatenate two lists element wise python
concat elements of a list
python concatenate multiple lists
merge all elements of list python
python merge multiple lists
python join list elements with separator
combine elements of list into string

I have a list of items : eg:

a = ['when', '#i am here','#go and get it', '#life is hell', 'who', '#i am here','#go and get it',]

I want to merge the list items based on condition i.e merge all the items till the item has # in first place and replace it with when or who. The output I want is :

['when', 'when i am here','when go and get it', 'when life is hell', 'who', 'who i am here','who go and get it',]

You can iterate over a, save the word if it does not start with'#', or replace '#' with the saved word if it does:

for i, s in enumerate(a):
    if s.startswith('#'):
        a[i] = p + s[1:]
    else:
        p = s + ' '

a becomes:

['when', 'when i am here', 'when go and get it', 'when life is hell', 'who', 'who i am here', 'who go and get it']

Python, List comprehension does the task of concatenating the similar index elements. The task of mapping each index element with each other is performed by map  In this step, you should create a User Defined Function to combine the names based on the unique ID numbers, please hold down the ALT + F11 keys, and it opens the Microsoft Visual Basic for Applications window. 4. Click Insert > Module, and paste the following code in the Module Window. VBA code: concatenate text based on criteria

Just going off the info you provided, you could do this.

    a = ['when', '#i am here','#go and get it', '#life is hell', 'who', '#i am here','#go and get it']
    whoWhen = ""                                                 #are we adding 'who or when'
    output = []                                                  #new list
    for i in a:                                                  #loop through
        if " " not in i:                                         #if there's only 1 word
            whoWhen = i + " "                                    #specify we will use that word
            output.append(i.upper())                             #put it in the list
        else:                           
            output.append(i.replace("#", whoWhen))               #replace hashtag with word
    print(output)

Prints:

['WHEN', 'when i am here', 'when go and get it', 'when life is hell', 'WHO', 'who i am here', 'who go and get it']

Process returned 0 (0x0)        execution time : 0.062 s
Press any key to continue . . .

Python, Sometimes, we require to merge some of the elements as single element in the list. The join function can be coupled with list slicing which can perform the task of same element in first list · Python | Add list elements with a multi-list based on See your article appearing on the GeeksforGeeks main page and help other​  Method #1 : Using list comprehension + zip() List comprehension does the task of concatenating the similar index elements. The task of zip function is concatenating the resultant string into a single list and return list.

Here you go:

def carry_concat(string_list):
    replacement = ""  # current replacement string ("when" or "who" or whatever)
    replaced_list = []  # the new list
    for value in string_list:
        if value[0] == "#":
            # add string with replacement
            replaced_list.append(replacement + " " + value[1:])
        else:
            # set this string as the future replacement value
            replacement = value
            # add string without replacement
            replaced_list.append(value)
    return replaced_list

a = ['when', '#i am here','#go and get it', '#life is hell', 'who', '#i am here','#go and get it',]

print(a)
print(carry_concat(a))

This prints:

['when', '#i am here', '#go and get it', '#life is hell', 'who', '#i am here', '#go and get it']
['when', 'when i am here', 'when go and get it', 'when life is hell', 'who', 'who i am here', 'who go and get it']

How To Conditionally Concatenate A Range, Condition – This is the condition to test. What It Does. This formula will conditionally concatenate a range based on a criteria in another range. Asking for help, clarification, or responding to other answers. Making statements based on opinion; back them up with references or personal experience. To learn more, see our tips on writing great answers.

Merge, join, and concatenate, Unlike list.append method, which appends to the original list and returns nothing, better) than other open source implementations (like base::merge.data.frame in R). right_index : Same usage as left_index for the right DataFrame Another fairly common situation is to have two like-indexed (or similarly indexed) Series  Hold Ctrl + Shift then press Enter while in Edit Mode to create an array formula. For Mac, use ⌘ + Shift + Return . Range – This is range of values which we want to concatenate together. Delimiter – This is the delimiter value which we want to use to separate values by in our concatenation.

paste: Concatenate Strings, If a value is specified for collapse , the values in the result are then concatenated into a single string, with the elements being separated by the value of collapse . Excel has some great built in functions for summing and counting conditionally based on given criteria, but to concatenate a range conditionally we will need to create our own user defined function. This ConcatenateIf function will concatenate a range of values based on a given criteria and separate them with a text delimiter of your choice.

Documentation: 9.1: Array Functions and Operators, You may want to view the same page for the current version, or one of the supported versions listed above instead. array-to-element concatenation, ARRAY[4,5,6] || 7, {4,5,6,7} optionally with lower bounds other than 1, array_fill(​7, ARRAY[3], ARRAY[2]), [2:4]={7,7,7} Conditional Expressions, Up, Aggregate Functions  Asking for help, clarification, or responding to other answers. Making statements based on opinion; back them up with references or personal experience. To learn more, see our tips on writing great answers.

Comments
  • Seems pretty straightforward. Can you show what you've tried and describe how it's failing?
  • show what have you tried
  • Why upper()? OP didnt mention anywhere
  • @mad_ Just for style, doesn't affect the functionality. I just thought it was hard to read where the keywords were in lowercase.