Post your Python related doubts/conversations here! Topics will be visible to your Teaching Assistants and Peers.
Your doubt will be solved within 24 hours!
Doubt in assignment !!!
Write a function to filter all odd numbers in the list and return the list containing only odd numbers.
My code:-
def fun(list):
for i in list:
if i%2==0:
list.remove(i)
return list
print(fun([5,6,3,2,8,9,1,7,4]))
My Output:-
[5, 3, 8, 9, 1, 7]
But my output is getting wrong because it is getti ng even number 8 also.
Expected output:-[5,3,9,1,7]
So can anyone please explain where am i going wrong?
Thank you!
don’t use list as a variable name because list is a keyword and it shouldn’t be used as a variable name.
Because when we are removing the elements of a list then the length of that list got changed again and again.
And for loop will work over the length of the list that’s why after removing the element when next iteration started the length will get reduced because of the deletion.
For getting odd numbers you need to create another list copy the content of first list to second and then use for loop with first list and remove from second list.
I have input all the thing correctly but the out out is not coming saying that syntax is invalid i don’t know wat to do plz help to solve out
https://www.hackerrank.com/challenges/detect-html-links/problem?isFullScreen=true&h_r=next-challenge&h_v=zen&h_r=next-challenge&h_v=zen&h_r=next-challenge&h_v=zen
Please help me solving this problem on Hackerrank.
And please rectify mistake in my solution below.
import re
N=int(input())
lines = []
for i in range(N):
lines.append(input())
for i in lines:
link=i.search(“^($”,i)
name=i.search(“^>.+<$”,i)
print(link.strip(),‘,’,name.strip())
There are lots of mistake in your code.
Try this code below: (put the indentation properly while running the code)
import re
for i in range(int(input().strip())):
data = input().strip()
matches = re.findall(r’[^<]<a href=“([^”]+)".?>(?:[^<]<\w+>)([^<]?)(?:</\w+>)*</a>', data)
if matches:
for m in matches:
print(“{0},{1}”.format(m[0].strip(), m[1].strip()))
#program to filter out all odd numbers from a list
L=[1,2,3,4,5,6,7,8,9,10]
for i in L:
if i%2==0:
L.remove(i)
else:
pass
print(L)