I have recently started trying to learn Python, and I try to improve the way I write code, and make it more "Pythonic". Therefore, it would really be nice if someone could explain to me if the following can be formulated more elegantly. Hopefully this is not a duplicate (I checked, but you never know)
I have a list of 5 elements, and I want to return specific elements. Let's say for example that I have [1, 2, 3, 3, 4]. I already have a function double(list), that if a list element exists twice returns that element (in this case 3).
I would like to generate from this list a tuple that contains the numbers that are exist only once (1, 2, 4).
One option is the following:
- Run the double(list) function and get the value of the element that is double.
- Create an empty tuple
- Iterate over the list items, and if the value is not equal to what the double(list) function returned, add it to the tuple
- Return the tuple.
My question is: is there a more elegant/Pythonic way of doing this? (in one line, using maybe a more complex expression?)
Thanks in advance