2
num = list(str(1234567))

for n1 in num:
    print(n1)
    for n2 in reversed(num):
        print('\t', n2)

On each iteration, it prints the first digit from the first loop and all 7 from the reverse loop. How can I print not all digits but only the last (i.e first) digit from reverse loop?

Thanks

5 Answers 5

7

Simplest way is to just zip the forward and reverse lists together:

for n1, n2 in zip(num, reversed(num)):
    print(n1, '\t', n2)
Sign up to request clarification or add additional context in comments.

5 Comments

izip = zip and %d = %s here. Yea, great solution.
Need from itertools import izip, but this also works with just zip().
I have to use Java and I think what you did was sufficiently advanced technology.
@Nimbuz, as ezod said, izip is from itertools. The difference is that it's more memory efficient if you have large lists. It doesn't create the whole list in one go, but instead generates it an element at a time.
Ok great, I didn't even know it existed, I thought it was a typo. :P
1

Here's a feeble attempt. Is this the kind of thing you're looking for?

 for idx,i in enumerate(x):
     print(i,"\t",x[-(idx+1)])

2 Comments

enumerate returns a list of tuples each element of which is the index (which I often abbreviate to idx) and the element. enumerate("abc") => [(0,'a'),(1,'b'),(2,'c')]
Oh great, nice to know about that, Thanks!
1

Do you mean like this?

num = list(str(1234567))
for i in range(len(num)):
    print(num[i], '\t', num[-(i+1)])

Output is:

1       7                       
2       6                       
3       5                       
4       4                       
5       3                       
6       2                       
7       1  

4 Comments

What does "num[-(i+1)]" do? and why not just "for i in num"?
for i in num iterates over num (touches every element in num). num[-(i+1)] is lookup of a specific element by index.
The negative index means to start from the end of the list. The i+1 is necessary because, for example, num[-1] is the last item in the list, yet the forward indexing (and thus, the range of values of i) begins with 0. Using for i in num will make i equal to the value of one of the items in num at each iteration, as opposed to the list index. range(len(num)) by contrast returns (basically) a list of indices for num.
Great, learnt a lot of new things from this one simple example, thanks all! :)
0

Nothing to do with Python, but here it is in Haskell :)

myDie  = [1,2,3,4,5,6]
sevens = [ (x,y) | x <- myDie, y <- myDie, x+y == 7]

1 Comment

Okay, but why weird variable names? :)
0

You should make a second list:

>>> num_rev = num[:]
>>> num_rev.reverse()
>>> num_rev
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]

Then do something like:

>>> for n1,n2 in zip(num,num_rev): 
...     print(n1, n2)
... 
0 9
1 8
2 7
3 6
4 5
5 4
6 3
7 2
8 1
9 0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.