+ 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)
4 Answers
+ 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)
+ 8
Yes, you can use range() function as well
https://code.sololearn.com/claufWm1nZv0/?ref=app
+ 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 !
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.