Home > Blockchain >  create several lists with position
create several lists with position

Time:02-10

I have this super simple code to generate a list in python. I want to make several lists like this

0: [0, 2]
1: [2, 4]
2: [4, 6]

it is possible . thanks

n = range(0,169,2)
num_list=list(n)
print (num_list)

CodePudding user response:

num_list = []
for n in range(0, 169, 2):
  num_list.append([n, n 2])

print(num_list)

Does the trick.

If you want them to have dictionary keys:

num_list = {}
for n in range(0, 169, 2):
  num_list[int(n/2)] = [n, n 2]

print(num_list)

Either way, num_list[48] returns [96, 98].

CodePudding user response:

>>> {n: [n*2, n*2 2] for n in range(3)}
{0: [0, 2], 1: [2, 4], 2: [4, 6]}

Adjust the range(3) to produce however many lists you want.

If you want to be able to adjust the length of the individual lists:

>>> {n: list(range(n*2, n*2 4, 2)) for n in range(3)}
{0: [0, 2], 1: [2, 4], 2: [4, 6]}
  • Related