+ 2

Is there a better way to solve this code challenge Python

Is there a better solution than mine? e.g. List Operations Write a program that takes a num number as input, makes list operations and outputs the updated list You are given a list of items. Write a program that takes a num number as input, reassigns the element with that index in the list to the value "x" and outputs the updated list. For example, for a given list [1, 2, 3, 4, 5] and input 3, the output should be: [1, 2, 3, "x", 5] #my solution items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] num = int(input()) if num in items or num >= 0: items[num] = 'x' print(items)

25th Jan 2021, 7:54 PM
FuLong
FuLong - avatar
4 ответов
+ 2
Why are you checking num in items if it's all about index ? Suppose num has a value 10 and even if 10 is in list ,the maximum index is 9 only. what I might do is as follow, if num>=0 and num<len(items): num[items]='x' print(items)
25th Jan 2021, 8:08 PM
Abhay
Abhay - avatar
+ 8
Yes, you can use range() function as well https://code.sololearn.com/claufWm1nZv0/?ref=app
26th Jan 2021, 2:40 AM
Simba
Simba - avatar
+ 1
Milan Peric but num is index number and you wanna make sure that index number is less than actual list length otherwise number won't be there to replace with "x" or maybe I am missing something as you sound pretty confident about it !! Glad I could help tho !
25th Jan 2021, 8:28 PM
Abhay
Abhay - avatar
0
Thanks appreciate it. I didn't know that len() is good for checking stuff like that that's why I used num in items.
25th Jan 2021, 8:15 PM
FuLong
FuLong - avatar