1

I've two list of string, I'm trying to append both list and would like to add pipe (|) delimiter at the end of string. Please find my code below:

firstList = ['Product1,item,,description,price|', 'Product2,item,,description,price|','Product3,item,,description,price|']
secondList = 'Product4,item,,description,price'

result = firstlist + secondList
result.append('|')
print result

I want to print my output like below:

Product1,item,,description,price|Product2,item,,description,price|Product3,item,,description,price|Product4,item,,description,price|

But my code is not appending the pipe delimiter property, its appending as a separate string value in the list. While appending new value in existing list I'm seeing blank space which I don't want.

Can someone please help to resolve this. It would be really helpful if someone can educate what's the efficient way of appending two list in python.

Thanks.

6
  • Are you guaranteed that the strings in the first list are already terminated by a pipe? (Note that a final character is a terminator not a delimiter.) Commented Nov 5, 2017 at 0:22
  • No I don't want to terminate the pipe from both the list. Commented Nov 5, 2017 at 0:24
  • I don't understand your previous comment. And it doesn't answer my question. Commented Nov 5, 2017 at 0:25
  • Do you mean secondList? Commented Nov 5, 2017 at 0:28
  • No, I mean the strings in firstList already end with pipes. Will this always be the case for every possible input to your program? Commented Nov 5, 2017 at 0:29

3 Answers 3

2

You can try this:

firstList = ['Product1,item,,description,price|', 'Product2,item,,description,price|','Product3,item,,description,price|']
secondList = secondList = 'Product4,item,,description,price'
final_list = ''.join(firstList+[secondList[0]+"|"]) if isinstance(secondList, list) else ''.join(firstList+[secondList+"|"])

Output:

'Product1,item,,description,price|Product2,item,,description,price|Product3,item,,description,price|Product4,item,,description,price|'
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks it worked. Is there any option to remove the square bracket and quotes from final output. I've update my output format.
@learnjquery you cannot remove the quotes as the value itself is a string. However, please see my recent edit to create a stand-alone string.
its working but after pipe delimiter I'm seeing comma(,) is there any way to remove that too
This assumes that secondList contains exactly one string. A more robust solution will iterate over all the elements of secondList.
Great! Thank you once again
|
2
[itm + '|' if not itm.endswith('|') else itm for itm in firstList + secondList]

You can use list comprehension to do that with if-else condition in it

Comments

1

It seems you want to do two different things here. Address each of them separately:

  1. Append '|' to the end of each string in a list. You can do this with a list comprehension.

  2. Concatenate two lists. You can do this with the + operator.

I leave the details as an exercise for the reader. Now that you have some terminology, you can google for more information.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.