Define a Python function remdup(l) that takes a nonempty list of integers l and removes all duplicates in l, keeping only the first occurrence of each number.
Answers
Python function:
A function is a block of code that merely runs when it is called a python function.
Following is the python function remdup(I) that takes a non-empty list of integer I and removes all duplicates in I, by keeping only first occurrence of each number:
Here’s is code:
def remdup(l):
return(myremdup(l,[]))
def myremdup(l,s):
if l == []:
return([])
else:
if l[0] in s:
return(myremdup(l[1:],s))
else:
return([l[0]]+myremdup(l[1:],s+[l[0]]))
Hope it helped...
Defining a Python function remdup(l) that takes a non-empty list of integers and removes all duplicates.
#Method 1: ORDERED OUTPUT (Dynamic and NOT using 'set' function directly )
def remdup(x):
l=[]
for i in x:
if i not in l:
l.append(i)
return l
x=[]
for i in range(int(input())):
x.append(int(input()))
print(remdup(x))
Input for program 1:
9
10
-1
6
51
-2
51
48
-1
6
Output for program 1:
[10, -1, 6, 51, -2, 48]
#Method 2: UNORDERED OUTPUT (Dynamic and using 'set' function directly )
x=[]
def remdup(x):
return list(set(x))
for i in range(int(input("Enter the list range: "))):
x.append(int(input("Enter an element into the list:")))
print(remdup(x))
#Method 3: UNORDERED OUTPUT (Dynamic and not using the 'set' function)
x=[]
def remdup(x):
for i in x:
if x.count(i)>1:
x.remove(i)
return x
for i in range(int(input("Enter the list range: "))):
x.append(int(input("Enter an element into the list:")))
print(remdup(x))
Input for both the programs:
Enter the list range: 9
Enter an element into the list:10
Enter an element into the list:-1
Enter an element into the list:6
Enter an element into the list:51
Enter an element into the list:-2
Enter an element into the list:51
Enter an element into the list:48
Enter an element into the list:-1
Enter an element into the list:6
Output for program 2:
[6, 10, 48, 51, -1, -2]
Output for program 3:
[10, -2, 51, 48, -1, 6]
Learn more:
1) Printing all the palindromes formed by a palindrome word.
brainly.in/question/19151384
2) Write a Python function sumsquare(l) that takes a non-empty list of integers and returns a list [odd, even], where odd is the sum of squares all the odd numbers in l and even is the sum of squares of all the even numbers in l.
brainly.in/question/15473120
3) Python program to find the absolute difference between the odd and even numbers in the inputted number.
brainly.in/question/11611140