Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python

having trouble in my program

hello friend,i want to know that i want to change a thing in my list .the code is below

my=[(0,1),(0,2),(0,3)]

i want to change the 0 in list by adding 1 in each 0 .please tell me.

1 Answer

I'm not sure what you mean - do you want to change it while the program is running? Why can't you just change it in the code before you run the program? my is a list of tuples - you can't change tuples the way they are (they're immutable). The only open would be to loop through and create new tuples.

my=[(0, 1), (0, 2), (0, 3)]

my2 = my #copy my into a new list
my = [] #clear my
for element in my2: #loop through the old elements
    first = element[0]+1  #you can't change the element itself, so we'll get the value and make a new one
    second = element[1]  #it's not necessary to create a new variable for second, but it's clearer
    my.append((first, second)) #add a new tuple made of the old values

thank you so much .you don't know from what problem you escaped me.