-2

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)
wjandrea
  • 28,235
  • 9
  • 60
  • 81
youngt17
  • 55
  • 7
  • 4
    What's your question? Did you mean to write _"**is it** possible **?**"_? – wjandrea Feb 09 '22 at 21:02
  • 3
    Is *what* possible? – martineau Feb 09 '22 at 21:04
  • 1
    What have you tried, and what research have you done? For example, if you want to break up `num_list`, have you read this question? [Splitting a Python list into a list of overlapping chunks](/q/36586897/4518341). For tips, see [ask]. – wjandrea Feb 09 '22 at 21:06
  • Thanks a lot everyone. I'm learning python. Sorry for the dummy question – youngt17 Feb 09 '22 at 21:11

2 Answers2

1
>>> {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]}
Samwise
  • 68,105
  • 3
  • 30
  • 44
1
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].

Ethan Ray
  • 13
  • 4