How to increment index in Python loop
Question:
I am wanting to loop through a string and capture 2 items each time while also incrementing through the index of the iterable. So I want to slice 2 items but increase the index by 1 every time through the loop. How can I do this?
my_string = ‘teststring’
desired output =
te
es
st
ts
st
tr
ri
in
ng
I have tried the following to slice the two items, but can’t figure out the best way to iterate thought the index
str1 = 'teststring'
i=0
while i<10:
i +=1
str2=str1[0:2]
print(str2)
Answers:
Here is a possible solution (s
is your string):
for j in range(len(s) - 1):
print(s[j:j + 2])
Another one:
for c1, c2 in zip(s[:-1], s[1:]):
print(c1 + c2)
str1 = 'teststring'
result = []
for i in range(len(str1) - 1):
result.append(str1[i:i + 2])
print(result)
output
['te', 'es', 'st', 'ts', 'st', 'tr', 'ri', 'in', 'ng']
Since you are trying to move both the start and end point of the slice with each iteration, you want to use the index+length of slice for the end point.
You should iterate after the slice is done. Here is the answer with minimal changes to your code:
str1 = 'teststring'
i=0
while i<=len(str1)-2:
str2=str1[i:i+2]
i += 1
print(str2)
I would use a for loop instead of a while loop, like this:
def strangeSlice(string):
out = ""
for i in range(len(string)-1):
out += string[i:i+2]
if i != len(string)-2:
out += " "
print(out)
return out
def main():
strangeSlice("teststring")
main() #you need to call the main function to run
By using list comprehension you can do this in a one-liner
s = 'teststring'
r = ' '.join([s[i:i+2] for i in range(len(s)-1)])
print(r)
An ITERABLE is:
anything that can be looped over (i.e. you can loop over a string or file) or
anything that can appear on the right-side of a for-loop: for x in iterable: … or
anything you can call with iter() that will return an ITERATOR: iter(obj) or
an object that defines iter that returns a fresh ITERATOR, or it may have a getitem method suitable for indexed lookup.
from linked_list import LinkedList, LinkedListIterator
class TVChannel:
def _init_(self):
self.full = LinkedList()
self.favs = LinkedList()
self.channel_tr = LinkedList()
def add_channel(self, number, name, favorite):
mov = {
"number": number,
"name": name
}
self.full.insert_back(mov)
if favorite:
self.favs.insert_front(name)
if self.channel_tr is None:
self.channel_tr = LinkedListIterator(self.full)
def current_channel(self):
return self.channel_tr.current_value()['number']
def up_channel(self):
self.channel_tr = self.channel_tr.next()
def all_channels(self):
return self.full.get_iterator()
def favorite_channels(self):
return self.favs.get_iterator()
I am wanting to loop through a string and capture 2 items each time while also incrementing through the index of the iterable. So I want to slice 2 items but increase the index by 1 every time through the loop. How can I do this?
my_string = ‘teststring’
desired output =
te
es
st
ts
st
tr
ri
in
ng
I have tried the following to slice the two items, but can’t figure out the best way to iterate thought the index
str1 = 'teststring'
i=0
while i<10:
i +=1
str2=str1[0:2]
print(str2)
Here is a possible solution (s
is your string):
for j in range(len(s) - 1):
print(s[j:j + 2])
Another one:
for c1, c2 in zip(s[:-1], s[1:]):
print(c1 + c2)
str1 = 'teststring'
result = []
for i in range(len(str1) - 1):
result.append(str1[i:i + 2])
print(result)
output
['te', 'es', 'st', 'ts', 'st', 'tr', 'ri', 'in', 'ng']
Since you are trying to move both the start and end point of the slice with each iteration, you want to use the index+length of slice for the end point.
You should iterate after the slice is done. Here is the answer with minimal changes to your code:
str1 = 'teststring'
i=0
while i<=len(str1)-2:
str2=str1[i:i+2]
i += 1
print(str2)
I would use a for loop instead of a while loop, like this:
def strangeSlice(string):
out = ""
for i in range(len(string)-1):
out += string[i:i+2]
if i != len(string)-2:
out += " "
print(out)
return out
def main():
strangeSlice("teststring")
main() #you need to call the main function to run
By using list comprehension you can do this in a one-liner
s = 'teststring'
r = ' '.join([s[i:i+2] for i in range(len(s)-1)])
print(r)
An ITERABLE is:
anything that can be looped over (i.e. you can loop over a string or file) or
anything that can appear on the right-side of a for-loop: for x in iterable: … or
anything you can call with iter() that will return an ITERATOR: iter(obj) or
an object that defines iter that returns a fresh ITERATOR, or it may have a getitem method suitable for indexed lookup.
from linked_list import LinkedList, LinkedListIterator
class TVChannel:
def _init_(self):
self.full = LinkedList()
self.favs = LinkedList()
self.channel_tr = LinkedList()
def add_channel(self, number, name, favorite):
mov = {
"number": number,
"name": name
}
self.full.insert_back(mov)
if favorite:
self.favs.insert_front(name)
if self.channel_tr is None:
self.channel_tr = LinkedListIterator(self.full)
def current_channel(self):
return self.channel_tr.current_value()['number']
def up_channel(self):
self.channel_tr = self.channel_tr.next()
def all_channels(self):
return self.full.get_iterator()
def favorite_channels(self):
return self.favs.get_iterator()