-1

I'm trying to pick 8 values from the list and assign them to variables. How to simplify it, make it more efficient.

I was not enough specific. I would like to pick 8 values from the beginning of the list, make operation, add it to result, then pick next 8 number from the list and so on until the end of the list

input_data = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]
index=0
for n in range(int(len(input_data)/8)):
        v1=input_data[n+index]
        x1=input_data[n+1+index] 
        y1=input_data[n+2+index]
        z1=input_data[n+3+index]
        v2=input_data[n+4+index]
        x2=input_data[n+5+index] 
        y2=input_data[n+6+index]
        z2=input_data[n+7+index]
        index+=7
        print(v1,x1,y1,z1,v2,x2,y2,z2)
Marcin
  • 21
  • 5
  • so assigning the last 8 elements of list to the variables? – DirtyBit Feb 15 '19 at 10:46
  • use a dictionary – Chris_Rands Feb 15 '19 at 10:48
  • If you're only printing them, you can just slice the list. – meowgoesthedog Feb 15 '19 at 10:48
  • I guess one optimization here off the bat is to use `range(1, 25)` instead of that list – Jab Feb 15 '19 at 10:48
  • 1
    As others have hinted, it is not at all clear just what you are trying to accomplish, so we are not sure what to change. What is your end goal here? (Which 8 values do you really want, what could the input data be, etc.) – Rory Daulton Feb 15 '19 at 10:49
  • 1
    If it is just for printing, you can use for n in range(0, len(input_data),6): print(input_data[n:n+7]) – fernand0 Feb 15 '19 at 10:50
  • Please see my [answer](https://stackoverflow.com/a/54707972/225020), as I also had a very similar [question](https://stackoverflow.com/questions/54374598/how-would-i-unpack-a-flat-list) which relates very closely. – Jab Feb 15 '19 at 11:12
  • 1
    Possible duplicate of [What is the most "pythonic" way to iterate over a list in chunks?](https://stackoverflow.com/questions/434287/what-is-the-most-pythonic-way-to-iterate-over-a-list-in-chunks) ... Also [How do you split a list into evenly sized chunks?](https://stackoverflow.com/q/312443/2823755) – wwii Feb 15 '19 at 16:33

6 Answers6

1

You cannot optimise variable assignment in terms of speed, but you can in terms of readability:

input_data = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]
for n in range(len(input_data)//8):
    v1, x1, y1, z1, v2, x2, y2, z2 = input_data[n*8:(n+1)*8]
Benoît P
  • 3,179
  • 13
  • 31
  • I was looking exactly this, it looks better, it's shorter. I need only to catch what is happening inside the square bracket – Marcin Feb 15 '19 at 11:00
  • Note the use of `//`. Integer division, this will not round up, which means if you have seven elements, it won't enter the loop. – Benoît P Feb 15 '19 at 11:03
0

If the goal is to only print it, you can do it using slicing:

start_index = -8
print(input_data[start_index:])

OR

If you want to assign them, you can use a list to store the values that can later be extracted separably.

vars_list = []
for i in range(start_index, 0):
    vars_list.append(input_data[i])


print(vars_list[0])
print(vars_list[1])
print(vars_list[2])
print(vars_list[3])
print(vars_list[4])
print(vars_list[5])
print(vars_list[6])
print(vars_list[7])

OUTPUT:

[17, 18, 19, 20, 21, 22, 23, 24]
17
18
19
20
21
22
23
24

EDIT:

Easier, one-liner killer:

start_index = -8
for i in range(len(input_data)//8): v1, x1, y1, z1, v2, x2, y2, z2 = input_data[start_index:]

print(v1, x1, y1, z1, v2, x2, y2, z2)

OUTPUT:

17 18 19 20 21 22 23 24
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
0

Assuming fixed length of input_data:

for i in range(3):
    v1,x1,y1,z1,v2,x2,y2,z2 = input_data[i*8:(i+1)*8]
    print(v1,x1,y1,z1,v2,x2,y2,z2)

Or indefinite length (but multiple of 8) of input_data:

for i in range(0,len(input_data), 8):
    v1,x1,y1,z1,v2,x2,y2,z2 = input_data[i:i+8]
    print(v1,x1,y1,z1,v2,x2,y2,z2)

Which all prints same as OP's one

1 2 3 4 5 6 7 8
9 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24
Chris
  • 29,127
  • 3
  • 28
  • 51
0

in python we can do this by

input_data = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24]
temp=input_data
randomly_picked=[]

for i in range(len(input_data)):
    v1,v2,v3,v4,v5,v6,v7,v8 = input_data[i:i+8]
Vaibhav gusain
  • 406
  • 3
  • 9
0

This improves on readability some:

input_data = range(1,25)
data = iter(input_data)
for v1, x1, y1, z1, v2, x2, y2, z2 in zip(*[data]*8):
     print(v1, x1, y1, z1, v2, x2, y2, z2)

prints:

1 2 3 4 5 6 7 8
9 10 11 12 13 14 15 16
17 18 19 20 21 22 23 24

I asked a question which was answered with very similar intentions.

Jab
  • 26,853
  • 21
  • 75
  • 114
0

You can do this:

from random import sample 
list = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24] 
values = sample(list, 8) 
for x in values:     
    print(x)

This will select randomly eight numbers from your list and the for loop will print them.

  • I don't believe this is what the OP wants. I thought the same though when I first read it. – Jab Feb 15 '19 at 11:09