Join strings inside a list depending on criteria?

Related searches

I have a list in the following format,

my_list = ['value1', 'value2#', 'value3#', 'value4', 'value5#']

I'm hoping to join the values up until the #, thanks

new_list = ['value1value2#' 'value3#' 'value4value5#']

you can combine string util endswith('#'):

my_list = ['value1', 'value2#', 'value3#', 'value4', 'value5#', 'value6']
new_list = []

cur = ''
for s in my_list:
    cur += s
    if s.endswith('#'):
        new_list.append(cur)
        cur = ''

if cur != '':  # add the rest
    new_list.append(cur)

output:

['value1value2#', 'value3#', 'value4value5#', 'value6']

How to easily concatenate text based on criteria in Excel?, Concatenate text based on criteria with Kutools for Excel Tip: In the above formula, A2:A15 is the list data range you want to extract Dim xResult As String. To combine the cells with line break, the following User Defined Function may help you. Function ConcatenateIf_LineBreak(CriteriaRange As Range, Condition As Variant, ConcatenateRange As Range, Optional Separator As String = ",") As Variant. Dim xResult As String. On Error Resume Next.

You can join everything and split it by "#"

first - your list has wrong format, you missed commas: correct way would be:

my_list = ['value1', 'value2#', 'value3#', 'value4', 'value5#']

if you not correct that, your list will concatenate (join) strings inside, so you will have 3 values instead of 5.

second - if it doesnt matter if word is ending with "#" then you can for example join everything and split it by "#":

[s+"#" for s in "".join(my_list).split("#") if s]

Concatenate cell values based on a condition [No VBA], Based on criteria Concatenate cell values based on a condition [No VBA] shows you data in column B and C. I want to concatenate adjacent value to size to do is to use the concatenate function to add all values into one text string. Use a drop down list to filter and concatenate unique distinct values. The Join function takes an array of strings and joins them using a delimiter string, to return a single string. The Split function takes a string and separates it at the delimiter, to return an array of strings.

 my_list = ['value1', 'value2#' 'value3#' 'value4', 'value5#']
 [item+"#" for item in "".join(my_list).split("#")]

output : ['value1value2#', 'value3#', 'value4value5#']

Use a drop down list to filter and concatenate unique distinct values, Based on criteria The animated image above shows drop down lists in column A and a UDF in column B, once function that extracts and concatenates values based on a drop down list value. Function Concat_Unique(Lookup_Value As String , Lookup_Column As Range, Concat_column As Range)� Description The Join-String cmdlet joins, or combines, text from pipeline objects into a single string. If no parameters are specified, the pipeline objects are converted to a string and joined with the default separator $OFS. By specifying a property name, the property's value is converted to a string and joined into a string.

How To Conditionally Concatenate A Range, This formula will conditionally concatenate a range based on a criteria in another range. How It Works. IF(ConditionRange=Condition,Range,””)� arr.join([separator]) Parameters separator Optional Specifies a string to separate each pair of adjacent elements of the array. The separator is converted to a string if necessary. If omitted, the array elements are separated with a comma (","). If separator is an empty string, all elements are joined without any characters in between them

This ConcatenateIf function will concatenate a range of values based on a given _ ConcatenateRange As Range, Optional Delimiter As String = ",") As End If ' Loop through cells in the criteria range For j = 1 To CriteriaRange. list of email addresses with a Y or N in the next column depending on if the� Create a lookup formula that uses multiple criteria to find information in a database or table of data by using an array formula in Excel. The array formula involves nesting the MATCH function inside the INDEX function.

Create Dependent Drop Down Lists. Limit the choices in an Excel drop down list, depending on the value in another cell. For example, select "Fruit" in cell B2, and "apple, banana, peach" appear in the dependent drop down list in cell C2.

Comments
  • If the last value in my_list doesn't end in # you lose the final value from the answer. See Damien's answer for the solution.
  • Yes, I missed that case. Thanks for letting me know.
  • I've also realised that if the last value in my_list doesn't end in # then Damien's answer will add one! This question is a good example of how hard it can be to code something that appears very simple.