Python Forum
join string lists - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: join string lists (/thread-27572.html)



join string lists - redminote4dd - Jun-11-2020

Hello!
I have a lists which I want to join with the second item of itself

for exmaple:

1=[A, B, C, D]

I want python to output:

1=[AB, BC, CD]

how can I manage that?


RE: join string lists - menator01 - Jun-11-2020

lst = ['a','b','c','d']
joinlst = ''.join(lst)

newlist = [joinlst[:2],joinlst[1:3], joinlst[2:4]]

print(newlist)
Output:
['ab', 'bc', 'cd']



RE: join string lists - DPaul - Jun-11-2020

If you start with:
1=[A, B, C, D]
you are going to have a hard time making this work.
- You need a legal name for your list
- If A,B,C,... are strings , they seem to have no value (or is it 'A'...).
We need a better starting point.
Paul


RE: join string lists - redminote4dd - Jun-11-2020

(Jun-11-2020, 02:06 PM)DPaul Wrote: If you start with:
1=[A, B, C, D]
you are going to have a hard time making this work.
- You need a legal name for your list
- If A,B,C,... are strings , they seem to have no value (or is it 'A'...).
We need a better starting point.
Paul

ok, so lets use this as the staring case:

anylist=['A', 'B', 'C', 'D']

i want to output:

newlist=['AB', 'BC', 'CD']


RE: join string lists - menator01 - Jun-11-2020

Another way
lst = ['a','b','c','d']
joinlst = ''.join(lst)
newlist = []
for i in range(len(joinlst)):
    newlist.append(joinlst[i:i+2])
newlist.pop()

print(newlist)
Output:
['ab', 'bc', 'cd']



RE: join string lists - GOTO10 - Jun-11-2020

I'm curious to hear from some of the more experienced coders on the forum what they think is the best way to do this. I understand that it's generally considered desirable to avoid using range(len(sequence)), but I'm having a hard time doing so without adding lines of code or checks I wouldn't need otherwise.

Here is what I came up with:
l = ['a', 'b', 'c', 'd']
newl = []

for i in range(len(l) -1):
    newl.append(''.join(l[i] + l[i+1]))

print(newl)



RE: join string lists - perfringo - Jun-11-2020

There is built-in module textwrap - Text wrapping and filling which have function wrap

>>> import textwrap
>>> lst = ['A', 'B', 'C', 'D']
>>> textwrap.wrap(''.join(lst), 2)
['AB', 'CD']
>>> lst = ['A', 'B', 'C', 'D', 'E']
>>> textwrap.wrap(''.join(lst), 2)
['AB', 'CD', 'E']



RE: join string lists - redminote4dd - Jun-11-2020

(Jun-11-2020, 02:33 PM)GOTO10 Wrote: I'm curious to hear from some of the more experienced coders on the forum what they think is the best way to do this. I understand that it's generally considered desirable to avoid using range(len(sequence)), but I'm having a hard time doing so without adding lines of code or checks I wouldn't need otherwise.

Here is what I came up with:
l = ['a', 'b', 'c', 'd']
newl = []

for i in range(len(l) -1):
    newl.append(''.join(l[i] + l[i+1]))

print(newl)

that worked perfectly for me. thank you


RE: join string lists - pyzyx3qwerty - Jun-11-2020

Not the best approach, but if you wanted, you could do
list1 = ["a","b","c","d"]
str1 = list1[0] + list1[1]
str2 = list1[1] + list1[2]
str3 = list1[2] + list1[3]
list2 = [str1,str2,str3]
print(list2)
And output :
Output:
['ab', 'bc', 'cd']
You could also not define list2 and print it, like:
print([str1,str2,str3])



RE: join string lists - Yoriz - Jun-11-2020

Assuming that if you only pass a list with one item it returns an empty list.
from string import ascii_uppercase


def adjacent_list(iterabe):
    stored_character = None
    for character in iterabe:
        if stored_character:
            yield ''.join((stored_character, character))
        stored_character = character


for index in range(1, 27):
    letters = (ascii_uppercase[:index])
    print(list(adjacent_list(letters)))
Output:
[] ['AB'] ['AB', 'BC'] ['AB', 'BC', 'CD'] ['AB', 'BC', 'CD', 'DE'] ['AB', 'BC', 'CD', 'DE', 'EF'] ['AB', 'BC', 'CD', 'DE', 'EF', 'FG'] ['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH'] ['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI'] ['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ'] ['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK'] ['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK', 'KL'] ['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK', 'KL', 'LM'] ['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK', 'KL', 'LM', 'MN'] ['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK', 'KL', 'LM', 'MN', 'NO'] ['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK', 'KL', 'LM', 'MN', 'NO', 'OP'] ['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK', 'KL', 'LM', 'MN', 'NO', 'OP', 'PQ'] ['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK', 'KL', 'LM', 'MN', 'NO', 'OP', 'PQ', 'QR'] ['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK', 'KL', 'LM', 'MN', 'NO', 'OP', 'PQ', 'QR', 'RS'] ['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK', 'KL', 'LM', 'MN', 'NO', 'OP', 'PQ', 'QR', 'RS', 'ST'] ['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK', 'KL', 'LM', 'MN', 'NO', 'OP', 'PQ', 'QR', 'RS', 'ST', 'TU'] ['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK', 'KL', 'LM', 'MN', 'NO', 'OP', 'PQ', 'QR', 'RS', 'ST', 'TU', 'UV'] ['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK', 'KL', 'LM', 'MN', 'NO', 'OP', 'PQ', 'QR', 'RS', 'ST', 'TU', 'UV', 'VW'] ['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK', 'KL', 'LM', 'MN', 'NO', 'OP', 'PQ', 'QR', 'RS', 'ST', 'TU', 'UV', 'VW', 'WX'] ['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK', 'KL', 'LM', 'MN', 'NO', 'OP', 'PQ', 'QR', 'RS', 'ST', 'TU', 'UV', 'VW', 'WX', 'XY'] ['AB', 'BC', 'CD', 'DE', 'EF', 'FG', 'GH', 'HI', 'IJ', 'JK', 'KL', 'LM', 'MN', 'NO', 'OP', 'PQ', 'QR', 'RS', 'ST', 'TU', 'UV', 'VW', 'WX', 'XY', 'YZ']