Question:
finding a duplicate in a list of strings.. python?
2011-03-09 20:10:11 UTC
suppose list1 = ['oranges','apple','oranges','oranges','apple']
suppose list2 = ['oranges','apple','oranges','oranges']

I want to return orange and apple for list1
and for list2 ... orange

how can i do this with no loops in python? just if statements and using recursion that way
Three answers:
Jack Trades
2011-03-09 20:14:04 UTC
There are many different ways to do this. Check out this link for at least 5 different solutions depending upon exactly what you're trying to do.



Edit: Yes, I know. What you need to do is almost exactly the same as removing duplicates. Except, instead of removing the duplicates, you accumulate the duplicates in a list and return that list.



Because of your requirements (no loops, just ifs) I'm going on the assumption that this is homework. You don't generally want to use recursion in Python as Python is not a tail-call optimized language and function calls are quite expensive.



If you look at those solutions (admittedly for a different problem) you will be able to see the solution for your problem. I can't, in good conscience, do your homework for you.
2016-11-08 06:56:42 UTC
Python Find Duplicates In List
leitner
2016-11-16 14:03:54 UTC
hi i could such as you to apply python dictionary. In dictionary you could save Key:value pair. This you ought to use to envision for reproduction via jst traversing the string only as quickly as.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...