1

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?

Mass17
  • 1,431
  • 1
  • 10
  • 23

1 Answers1

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

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