-2

I want to create a function that can be use to delete the non comon elements( I should delete Audi an Mercedes of two lists) :

marcas = [
(1, 'Audi'),
(2, 'Nissan'),
(3, 'Mercedes'),]
marcas2 = [] 

coches = [
{
    'modelo': 'Audi C3',
    'marca': 1,
    'precio': 25000,
    'ano': 2017,
}]


def delbrand(marcas):

for y in coches:
    for x in marcas:
        if y['marca'] == x[0]:
            if x not in marcas2:
                marcas2.append(x)
m = 0

for i in marcas:
    if i not in marcas2:
        del marcaslist[m]
    m = m+1

would you mind to help me with this question?

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • 3
    your identation is wrong, please fix. – Patrick Artner Dec 26 '18 at 16:31
  • 2
    `marcaslist` is undefined, please fix – Patrick Artner Dec 26 '18 at 16:32
  • 2
    All in all I do not really get what you need to do - please share an example of your data and what you want to look it afterwards. There are tons of examples/questions about how to remove X from Y when Z is True ... you might want to search SO again – Patrick Artner Dec 26 '18 at 16:33
  • 1
    for example: [remove-all-the-elements-that-occur-in-one-list-from-another](https://stackoverflow.com/questions/4211209/remove-all-the-elements-that-occur-in-one-list-from-another) and https://stackoverflow.com/questions/1235618/python-remove-dictionary-from-list and https://stackoverflow.com/questions/7484562/python-remove-dictionary-from-list-if-exists and ton's more – Patrick Artner Dec 26 '18 at 16:40

1 Answers1

0

It is not very clear what are you asking for. I modified your code a bit:

brands = ['Audi', 'Nissan', 'Mercedes']

cars = [
{
    'modelo': 'Audi C3',
    'marca': 1,
    'precio': 25000,
    'ano': 2017,
}]

not_existing_brands = set()

def find_not_common(brands, cars):

    for brand in brands:
        for car in cars:
            if brand not in car['modelo']:
                not_existing_brands.add(brand)

find_not_common(brands, cars)

print brands
print list(not_existing_brands)

for item in list(not_existing_brands):
    brands.remove(item)

# final brands list
print brands

Output:

['Audi', 'Nissan', 'Mercedes']
['Mercedes', 'Nissan']
['Audi']
Daedalus
  • 295
  • 2
  • 17
  • Thank you so much, I need to delete in the brand list the brands that are not in the cars list (mercedes and nissan). – Alvaro Dmin Dec 26 '18 at 20:05
  • I updated the code based on what you are asking for. If you think that my answer is helpful what exactly you need, just tick green ;) – Daedalus Dec 26 '18 at 20:13