-3

I'm just dabbling in a bit of Python. I want to append items inside a for loop only if it is not already appended thus creating a list of unique items.

fruits = ["Apple","Banana","Banana","Apple"]
newList = []
for i in fruits:
    newList.append([i, i.Color])

print(newList)

Note: Just assume .Color as a property of fruits for the sake of problem. I want to perform the unique-fication operation inside the loop itself.

Current Output

[["Apple","Red"], ["Banana","Yellow"], ["Banana","Yellow"], ["Apple","Red"]]

Desired Output

[["Apple","Red"], ["Banana","Yellow"]]
wjandrea
  • 28,235
  • 9
  • 60
  • 81
BPC
  • 1
  • 1
  • 3
    Does this answer your question? [Removing duplicates in lists](https://stackoverflow.com/questions/7961363/removing-duplicates-in-lists) – Stuart Sep 03 '22 at 13:57
  • 2
    If the order doesn't matter, just use a `set`. – Thierry Lathuille Sep 03 '22 at 13:57
  • just check before appending as if item not in list then append to the list – Ali Sep 03 '22 at 14:02
  • @Ali, my list of colors is being created inside the loop itself. So I'm not sure how to check duplicate inside the same loop. – BPC Sep 03 '22 at 14:04
  • Maybe you can use a `dict` insted the new `list`? – JacekK Sep 03 '22 at 14:16
  • Why are you using lists instead of tuples? `newList.append((i, i.Color))` would make a lot more sense to me. On the other hand, why even bother getting `i.Color` in this loop when you could just as easily uniquify first *then* loop through and get the `.Color`? Also, is the order important? If not (and if you switch to tuples), you can use a `set`, if yes, you can use a `dict` as an ordered set. – wjandrea Sep 03 '22 at 15:19
  • BTW, welcome to Stack Overflow! Please take the [tour] and read [ask] for tips like starting with your own research. – wjandrea Sep 03 '22 at 15:21
  • What does the actual Fruit class look like? Does it support comparison with other Fruit objects? Cause if it doesn't, this isn't going to work in the first place and you'll need to figure something else out. Please make a [mre]. Also, does it support hashing? That's important for the set-like solutions. – wjandrea Sep 03 '22 at 15:27
  • 1
    I forgot to clarify, if the Fruit class doesn't support comparison **and** those strings represent *different instances*, **then** it's not going to work. Also, what I'm essentially saying is, this example is missing too many details to make sense of it. – wjandrea Sep 03 '22 at 15:37

5 Answers5

1

You can either check for the presence before adding, or you can use a set, which is unique.

newSet = set()
for i in fruits:
    newSet.add(i.Color)

newList = list(newSet)
James
  • 32,991
  • 4
  • 47
  • 70
  • Sorry I had to modify the question a bit, but there set won't work for the list. fruits = ["Apple","Banana","Banana","Apple"] newSet = set() for i in fruits: newSet.add([i,len(i)]) print(list(newSet)) – BPC Sep 03 '22 at 14:09
0

A set proxy is obvious way to do it:

fruits = ["Apple","Banana","Mango","Apple","Berry","Strawberry"]
for i in fruits:
    newSet.add(i.Color)

print(list(newList))
dmikon
  • 21
  • 3
  • Sorry I had to modify the question a bit, but there set won't work for the list. fruits = ["Apple","Banana","Banana","Apple"] newSet = set() for i in fruits: newSet.add([i,len(i)]) print(list(newSet)) – BPC Sep 03 '22 at 14:08
  • Where does `newSet` come from in your code? – Chris Sep 03 '22 at 15:17
  • You introduced newSet in your code. newSet.add(i.Color). I was trying the same. – BPC Sep 03 '22 at 15:50
0
fruits = ["Apple", "Banana", "Banana", "Apple"]
color = {"Apple": "Red", "Banana": "yellow"}

newList = []
for i in fruits:
    if [i, color[i]] not in newList:
        newList.append([i, color[i]])

print(newList)

I created a dictionary of colors for myself to answer your question. If you have object of fruits just use i.color.

S.B
  • 13,077
  • 10
  • 22
  • 49
Ali
  • 376
  • 5
  • 16
0

Create a set from fruits which discards duplicates, then use a list comprehension to map it to lists with the "color" as well.

[[i, i.Color] for i in set(fruits)]
Chris
  • 26,361
  • 5
  • 21
  • 42
  • 1
    It's worth noting that this assumes OP's implied `Fruit` class is hashable and that equivalent instances hash the same. – wjandrea Sep 03 '22 at 15:31
0

If fruits is a list of Fruit class instances, something similar to the below:

from dataclasses import dataclass

@dataclass
class Fruit:
    name: str
    colour: str
    
    
fruits = [
    Fruit("Apple", "Red"),
    Fruit("Banana", "Yellow"),
    Fruit("Banana", "Yellow"),
    Fruit("Apple", "Red"),
]

Then to output a list of fruit name/colour pairs, you can do the following:

uniq_fruit_names = set()
new_list = []

for fruit in fruits:
    if fruit.name not in uniq_fruit_names:
        uniq_fruit_names.add(fruit.name)
        new_list.append([fruit.name, fruit.colour])
        
print(new_list)
[['Apple', 'Red'], ['Banana', 'Yellow']]
Arnoux
  • 226
  • 2
  • 9