0

I am looking for a way to get a cartesian product of a string in the following form,

text = 'school'

I want the result like this,

list_ = [(s,c),(c,h),(h,o),(o,o),(o,l)]

this is what I tried,

text = 'school'
list_=[]
for i in range(len(text)):
  while i < len(text)+1:
    print(text[i], text[i+1])
    list_.append((text[i], text[i+1]))
    i = i+1

I got the necessary list, yet throwing off some errors. Is there any elegant way to do this?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Mass17
  • 1,555
  • 2
  • 14
  • 29

1 Answers1

3
text = 'school'
list(zip(text, text[1:]))

Out[1]:
[('s', 'c'), ('c', 'h'), ('h', 'o'), ('o', 'o'), ('o', 'l')]
Alex
  • 1,118
  • 7
  • 7