0

How can I 'format' my iterator in xrange to add a "normalized" suffix? Like "nice_name_1","nice_name_2", etc. My current iterator adds by 3 since I am using other for loop inside. Thank you

objs = ['pencil','pen','keyboard','table','phone']



for i in xrange(0, len(objs), 3):

    #...

    for n, obj in enumerate(objs[i:i+3]):
        print '...'

    #...
    print 'nice_name_' + str(i)

# Result:
...
...
...
nice_name_0
...
...
nice_name_3

1 Answer 1

1
print 'nice_name_' + str(i // 3)

or preferably

print('nice_name_{}'.format(i//3 + 1))  # start counting at 1
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! why double '//' ?
'//' is integer division, the same as the default behavior of '/' in Python 2 - but in Python 3 '/' is floating-point division, ie 6 // 3 = 2 but 6 / 3 == 2.0

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.