I need some help with my homework for school. I am supposed to write a program in python that ask the person for a list of numbers, in my case:
list= eval(input("Input your list: "))
Input a list: [53,16,22,81,43,16,88,55,43,5]
You could use list comprehension with
count>1 as filter condition (and also
index to make sure only the first of those occurrences is taken):
inp = eval(input('Input your list:')) print([j for i, j in enumerate(inp) if inp.index(j) == i and inp.count(j) > 1])
list as a variable name, as it is an existing function in Python.
As stated by others: using
eval is frowned upon, certainly when the argument is provided by the user.
This just uses the most basic constructs:
result =  n = len(inp) for i in range(n): count = 0 for j in range(n): if inp[i] == inp[j]: if j < i: break count += 1 if count > 1: result.append(inp[i]) break print(result)