Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
join string lists
#1
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?
Reply
#2
lst = ['a','b','c','d']
joinlst = ''.join(lst)

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

print(newlist)
Output:
['ab', 'bc', 'cd']
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
Gaming Collection
Homepage
my-python
Reply
#3
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
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu)
Reply
#4
(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']
Reply
#5
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']
I welcome all feedback.
The only dumb question, is one that doesn't get asked.
Gaming Collection
Homepage
my-python
Reply
#6
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)
Reply
#7
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']
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#8
(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
Reply
#9
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])
pyzyx3qwerty
"The greatest glory in living lies not in never falling, but in rising every time we fall." - Nelson Mandela
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Reply
#10
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']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ''.join and start:stop:step notation for lists ringgeest11 1 432 Apr-08-2021, 04:29 PM
Last Post: BashBedlam
  The difference between os.path.join( and os.sep.join( Pedroski55 2 1,080 Nov-17-2020, 08:38 AM
Last Post: Pedroski55
  Partial convertion string to int in lists satellite89 6 1,487 Apr-22-2019, 08:50 PM
Last Post: Yoriz
  SQL select join operation in python(Select.. join) pradeepkumarbe 1 974 Feb-14-2019, 08:34 PM
Last Post: woooee
  Sorting list of lists with string and int Otbredbaron 6 2,084 May-07-2018, 06:04 AM
Last Post: buran
  How to create a long string by concatenating two lists nikhilkumar 11 4,526 Jul-12-2017, 05:36 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020