I dealt with each element of the list using the loop... But I want my new elements to substitute the whole list when I try print(list). How can I get that done? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I dealt with each element of the list using the loop... But I want my new elements to substitute the whole list when I try print(list). How can I get that done?

4th Dec 2016, 9:39 PM
Ayomide Oguntuase Mofolorunso
3 Answers
+ 2
list_1 = [1,2,3] for i in list_1: if i != 4: value_index = list_1.index(i) list_1[value_index] = 4 print(list_1) @ayomide - i am not 100℅ sure about ur requirement, the above code goes through the list elements, if each element satisfies condition, new element substitutes the old one. if this is not your requirement can u be more specific??
10th Dec 2016, 9:31 AM
raja shekar
raja shekar - avatar
0
@raja- thank you, it dealt with it but it was in three lines of output and all the elements became 4. Now examine the code: list_1=[1,2,3] for i in list_1: print(i+1) Now every element increases by 1(2,3,4). now when I write: print(list_1) I want my output to be [2,3,4] how do I do that?
10th Dec 2016, 12:24 PM
Ayomide Oguntuase Mofolorunso
0
@ayomide - print elements out side of loop. list_1=[1,2,3] for I in list_1: list_2 = I + 1 print(list_2) this will deal with only increment elements in list. but if u wanna substitute specific code , the code in my first works. you have to print out side of loop that's it.
13th Dec 2016, 3:48 AM
raja shekar
raja shekar - avatar