0

I want to add a loop to the end of another loop. But all I've tried failed and I don't know what to do

I've tried

for i in range(2):
   print i
   for r in range(4, 6):
     print i.append(r)

and

for i in range(2):
   print i
   print i.append(4,5)

I expected:

0
1
4
5

But I received the following error instead:

Error for the 1st code sample:

Traceback:(most recent call last):
  File "main.py", line 4, in <module>
    print i.append(r)
AttributeError: 'int object has no attribute 'append'

Error for the 2nd code sample:

Traceback:(most recent call last):
  File "main.py", line 4, in <module>
    print i.append(4, 5)
AttributeError: 'int object has no attribute 'append'
3
  • 3
    range() gives you a list of integers, and iterating over that gives you each integer. Integers are not lists, so you can't append to them. What is it you're trying to accomplish? Commented Aug 1, 2019 at 18:59
  • 2
    And when you append to a list, the returned value is simply None, so nothing would print anyway. i = list(); for _ in range(2): for r in range(4, 6): i.append(r) Commented Aug 1, 2019 at 19:01
  • 1
    Create an empty list at before for loop, then append i, then add another separate for loop for r and append r to i using i.append(r) Commented Aug 1, 2019 at 19:01

4 Answers 4

4

If all you're trying to do is print all values of 2 ranges in succession then just add them together.

for i in range(2) + range(4, 6):
   print i

Results are as follows:

0
1
4
5

For Python3 you must either wrap the ranges in list or for a more elegant and forward compatible solution use using itertools.chain it combines both ranges making them one iterable.

for i in chain(range(2), range(4, 6)):
   print(i)

If what you're trying to do is make both ranges into a list then chain works as well:

list(chain(range(2), range(4, 6)))
#[0, 1, 4, 5]

Or just add them together

#py2
range(2) + range(4, 6)
#py3
list(range(2)) + list(range(4, 6)) 
Sign up to request clarification or add additional context in comments.

7 Comments

Or just plain range(2) + range(4, 6) does the same thing.
True, I've been using Python3 for way too long.
For Python3, you could do list(range(2)) + list(range(4, 6))
@FredLarson the chain() method is better (more memory efficient) and Python 2's lifetime is almost over
Yes you could although I do prefer the chain method as the line is more readable and efficient plus allows for more ranges
|
2

The other answers are good as well, but if you are expecting a list, this is what you would want to try to do without using imports.

lst = []
for i in range(2):
   lst.append(i)
for r in range(4, 6):
   lst.append(r)
print lst 

Comments

1

This code produces the output you seem to require:

for i in range(2):
   print i
for r in range(4, 6):
  print r

4 Comments

I think he is requesting a single list for both i and r values
What makes you say that? The expected output seems clear.
Only because of the .append in his code. I may be mistaken though.
Ok, I see what you did there. Your answer deserves an up-vote, even though the output is not as requested.
-1

your code doesn't work becoz i is a value in the first for loop and you can't append a value to a value.

Below for loops will get your desired result

for i in range(2):
   print(i)
for r in range(4, 6):
    print(r)

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.