5

I'm trying to perform the following:

tup1 = ()
for i in range(1, 10, 2):
    tup1 = (tup1, i)
print tup1

I expect the output to be the sequence 1 to 10. However, I end up with the following:

((((((), 0), 2), 4), 6), 8)

What would be a correct way to meet the requirement?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
abhishek nair
  • 324
  • 1
  • 6
  • 18
  • if you just want the sequence from 1-10, just have somethign like `print(list(range(1,11)))` – R Nar Nov 05 '15 at 19:25
  • 2
    Okay, but what are you actually trying to *do*? – Ignacio Vazquez-Abrams Nov 05 '15 at 19:25
  • I hope that you are aware that Tuples are immutable and are not supposed to be changed – Ryan Nov 05 '15 at 19:26
  • True.. could have done that.. However the objective is to append the values to the tuple/list and then print the output... – abhishek nair Nov 05 '15 at 19:27
  • @RNar what's the difference between `range(1,11)` and `list(range(1,11))`? – Pierre L Nov 05 '15 at 19:29
  • @abhisheknair if that is the case, then what you are doing wrong is you are making nested tuples instead of concatenating tuples. you should instead have `tup1 = tup1 + (i,)` to set `tup1` as a new tuple that consists of itself and and the new item in the form of a single item tuple – R Nar Nov 05 '15 at 19:31
  • @PierreLafortune, range(1,11) returns a range object. try printing it and you'll see what i mean – R Nar Nov 05 '15 at 19:32
  • @RNar They both print the same output `print range(1,11) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]` and `print list(range(1,11)) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]` – Pierre L Nov 05 '15 at 19:33
  • Thanks RNar! Could you please elaborate in detail on how it works ? – abhishek nair Nov 05 '15 at 19:33
  • @RNar I think you're talking about `xrange` – Mr. E Nov 05 '15 at 19:33
  • 1
    @Mr.E, nope i am not. try running this line `print(range(1,11))` and you will see what i mean (at least in python 3) – R Nar Nov 05 '15 at 19:35
  • By the way this question is duplicated: http://stackoverflow.com/questions/2309329/inserting-an-item-in-a-tuple – rfloresx Nov 05 '15 at 19:35
  • @abhisheknair, both otrebor and Tomasz have posted answers with concatenating tuples – R Nar Nov 05 '15 at 19:36
  • @RNar except tomaz just edited to include the tuple concatenation which is an identical copy of Ortrebors answer 5 mins ago – John Ruddell Nov 05 '15 at 19:37
  • 3
    @PierreLafortune and Mr. E, i apologize for the confusion, i am using python 3 not python 2! that is my bad – R Nar Nov 05 '15 at 19:37
  • @RNar Well you're right in Python 3, in Python 2 it returns a list. Weird change there. Don't worry, it happens, I learned something new about ranges :D – Mr. E Nov 05 '15 at 19:38
  • @Mr.E and python 3 is the future so best to code with that in mind :) – John Ruddell Nov 05 '15 at 19:38

9 Answers9

22

If you just want an iterable with the even numbers 1 to 10 then the simplest way to do it:

seq = range(2, 11, 2)

If you are doing this as a means of learning Python and you want to build up your own data structure, use a list:

l = []
for i in range(2, 11, 2):
    l.append(i)

The above for loop can be rewritten as a list comprehension:

l = [i for i in range(2, 11, 2)]

or using an if clause in the loop comprehension:

l = [ i for i in range(1, 11) if i % 2 == 0]
chucksmash
  • 5,777
  • 1
  • 32
  • 41
  • List comprehension is the way to go here. Tuples are immutable and not supposed to be changed / concatenated. so +1 for that and the pythonic way to do it :) – John Ruddell Nov 05 '15 at 19:40
6

You can append an item to a tuple using the += operator.

tup1=()
for i in range(1,10,2):
   tup1+= (i,)
print tup1

This prints (1, 3, 5, 7, 9)

rfloresx
  • 111
  • 5
3

Read about List Comprehension

tuple(i for i in range(1, 10, 2))

Or

tup1 = ()
for i in range(1, 10, 2):
 tup1 += (i,)
print tup1
Tomasz Jakub Rup
  • 10,502
  • 7
  • 48
  • 49
2

Tuples are immutable objects in Python. Thus means you can't modify them. What you're doing right now is creating a new tuple with the previous one inside

You could do:

lst = []
for i in range(1,10,2):
  lst.append(i)
tup = tuple(lst) #If you really want a tuple
print tup

But lst = range(1,10,2) or tup = tuple(range(1,10,2)) is much better (Unless you want to use append for some reason)

Mr. E
  • 2,070
  • 11
  • 23
1

it's something like this:

print range(1, 11)
Raul Cabrera A.
  • 169
  • 2
  • 6
  • Please add some explanation about the output of your command. Also, the question might no target to create a specific ordered range, but how to concatenate several values in a for-loop. – Nippey Nov 06 '15 at 08:58
1

You are skipping by two by using for i in range(1,10,2): if you use for i in range(1,11): if will increment by 1. As for tup1=(tup1,i) you are constantly adding a tuple to each other which is creating the weird output. You could use a list if you want to store them. Otherwise using will do it just fine:

print(range(10))
Cam Beatty
  • 78
  • 9
0

List item

For appending into list or tuple you can use append() function or you can use += operator which does the same. s=()

for sequence of numbers from 1 to 10

for i in range(1,11): s+=(i,)

print(s) #(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)

for sequence of numbers from 1 to 10 with step size 2

x=()

for i in range(1,11,2): x+=(i,)

print(x) #odd nos from 1-9 (1, 3, 5, 7, 9)

x=()

for i in range(2,11,2): x+=(i,)

print(x) #even nos from 2-10 (2, 4, 6, 8, 10)

  • List item
0

Storing values from loop in a list or tuple in Python by following ways -

-> By appending the value in the list (here new_data1) as join will not work here.

new_data1 = []
for line in all_words:
    new_data=' '.join(lemmatize_sentence(line))
    new_data1.append(new_data)
    #print (new_data)
print (new_data1)

P.S. - This is just a snapshot of a code just for hint . Hope this helps!!

0

This worked for me:

lst = []  # Create an Empty list
for i in range(0, 10, 2):
    lst.append(i)  # Add the values after each iteration to the list
print(lst)
juanpethes
  • 831
  • 2
  • 18