Retrieving the elements of a loop | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 3

Retrieving the elements of a loop

How can I retrieve all the elements of a "For" loop and put it in a list ??? For example: seq = "ACGTGCTATTCAGCTGGCTATGC" for i in range(len(sequence)-1): mot = sequence[i] + sequence[i+1] print(mot) Output: AC CG GT TG GC ... I want to retrieve all and add them in a list.

15th Sep 2020, 11:38 AM
Usmaan NgOm
Usmaan NgOm - avatar
6 Antworten
+ 11
This sample can be used to do the job, and accepts also other chunk sizes: txt = 'ABCDEFGHIJKLIMNOQRSTUVWXYZ' wd = 2 # this defines the chunk size for i in range(0, len(txt),wd): print(f'{txt[i:i+wd]}') if you want the text chunks to output in a list, this can be helpful: txt = 'ABCDEFGHIJKLIMNOQRSTUVWXYZ' wd = 2 # this defines the chunk size out2 = [f'{txt[i:i+wd]}' for i in range(0, len(txt),wd)]
15th Sep 2020, 5:21 PM
Lothar
Lothar - avatar
15th Sep 2020, 7:09 PM
Vitaly Sokol
Vitaly Sokol - avatar
+ 1
Bhavya Sarraf thanks it works !!!
15th Sep 2020, 12:39 PM
Usmaan NgOm
Usmaan NgOm - avatar
+ 1
Lothar thank u !!!
15th Sep 2020, 6:21 PM
Usmaan NgOm
Usmaan NgOm - avatar
0
15th Sep 2020, 11:42 AM
Usmaan NgOm
Usmaan NgOm - avatar
0
♤♢☞ 𝐊𝐢𝐢𝐛𝐨 𝐆𝐡𝐚𝐲𝐚𝐥 ☜♢♤ I think that my question was not so clear, so I edit it, you can check it again
15th Sep 2020, 12:26 PM
Usmaan NgOm
Usmaan NgOm - avatar