2

I have the following two lists:

x = [1,2] y = [4,5,6]

I want to iterate x by z.

I have a variable called code set to NONE and another variable called value also set to NONE. Here is the output I am aiming for:

1st iteration, code = 1 and value = 4
2nd iteration, code = 1 and value = 5
3rd iteration, code = 1 and value = 6
4th iteration, code = 2 and value = 4
5th iteration, code = 2 and value = 5
6th iteration, code = 2 and value = 6

Here is what I have tried:

x = [1, 2]
y = [4, 5, 6]

code = None
value = None


for x_ids, y_ids in zip(x, y):
    code = x_ids
    value = y_ids
    print("c", code)
    print("v", value)

output: 
c 1
v 4
c 2
v 5

Can anyone suggest how to get the output described above?

FSDford
  • 448
  • 5
  • 10
ApacheOne
  • 245
  • 2
  • 14
  • It's a bit unclear what you are asking for. To me in your expected output you are iterating once through all values in y for every iteration in x. I don't see a z here, and is this really what you want? – atru Oct 13 '20 at 22:23
  • my apologies, perhaps my code is incorrect which is why I am asking for help and the z value is not there, again I am sorry if I didn't explain this well enough. The ultimate goal and combination of how to iterate through are, what was written at the top: 1st iteration, code = 1 and value = 4 2nd iteration, code = 1 and value = 5 3rd iteration, code = 1 and value = 6 4th iteration, code = 2 and value = 4 5th iteration, code = 2 and value = 5 6th iteration, code = 2 and value = 6 – ApacheOne Oct 13 '20 at 22:55
  • No problem. So it is the case that you always iterate through y, for each value in x? – atru Oct 13 '20 at 23:46
  • That is exactly right, I want to iterate through y, for each value in x. I gotta say that is a much more efficient way to say what I explained earlier in my example, so thank you. – ApacheOne Oct 14 '20 at 00:05
  • Glad to help :) better formulation comes quickly with practice, answering now. – atru Oct 14 '20 at 00:08
  • Replace `zip` with `itertools.product` and it'll do what you want. – ShadowRanger Oct 14 '20 at 01:29

1 Answers1

3

This is one way to achieve what you're looking for:

x = [1, 2]
y = [4, 5, 6]

code = None
value = None

iter_count = 0
for x_ids in x:
    code = x_ids
    for y_ids in y:
        iter_count += 1
        value = y_ids
        print('{} iteration, code = {} and value = {}'.format(iter_count, code, value))
        #print(str(iter_count) + ' iteration, code = ' + str(code) + 'and value = ' + str(value))

Like discussed in the comments, this code iterates through all elements of y for every element in x. In your original code, you were iterating through both lists all at ones, using zip. Since you want to print the number of iteration too, there is a new variable, iter_count, that counts and stores those.

The code has two print statements, they print the same messages. The commented out one concatenates strings, and numbers converted to strings. The uncommented one may be less intuitive but it is often more useful and cleaner. It's worth looking at it, you can find an introduction here.

Last thing, if you need that too - to print numbers in 1st, 2nd etc. format you can use some of these approaches.

atru
  • 4,699
  • 2
  • 18
  • 19
  • Really appreciate you taking the time, I think what I learned most here is understanding what to search. When you wrote it out it really got to the point I was looking for. The code does exactly what I am looking for. Again thank you! – ApacheOne Oct 14 '20 at 00:19
  • Glad it helped and that you learned a new thing :) it's an important skill, you'll get there in no time, the main thing is to figure out the language. That again, comes with some practice/knowledge of the language. Good luck :) – atru Oct 14 '20 at 00:21