Python Forum

Full Version: create list from repeated pattern in python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,
I would like to create a list in Python3, which look like this

L = [(0,(0,1,2,3,4)), (1, (5,6,7,8,9)),(2,(10,11,12,13,14))......) 
lets call it
L= [(i,(j1,j2,j3,j4,j5),...)
The important is that the pattern keep on repeating till the j5 reaches 740231 Huh

Any suggestions would be very much appreciated.

Many thanks,
You could use list comprehension
L = [pattern(i) for i in range(n)]
Quote:L = [(0,(0,1,2,3,4)), (1, (5,6,7,8,9)),(2,(10,11,12,13,14))......)

I'm not going to go all the way out to 740 thousand, but it is a simple set of fors
import pprint

a_list=[]
inner_ctr=1

## use a while instead of this for and exit
## when the condition is met
for ctr in range(10):
    sub_list=[]
    for ctr_2 in range(5):
        sub_list.append(inner_ctr)
        inner_ctr += 1
    a_list.append((ctr+1, tuple(sub_list)))

pprint.pprint(a_list)