Home > Blockchain >  LIST DESCRIPTION NOT WORKING WITH DICTIONARY (PYTHON)
LIST DESCRIPTION NOT WORKING WITH DICTIONARY (PYTHON)

Time:09-10


#python
pets=[] #created an empty list 

d={'owner_name': 'holland',
     'pet_kind':'mischieveous',
  }


d={'owner_name': 'rowman',
       'pet_kind':'smart',
  }


d={'owner_name': 'clark',
         'pet_kind':'happy'
  }

d={'owner_name': 'shaun',
             'pet_kind':'shy',
  }

d={'owner_name': 'seaman',
         'pet_kind':'intellectual',
  }

pets=[pets.append(pet) for pet in d.items()] 

print(pets) 

output is showing [None, None] , I believe it should show the dictionary #appended in pets but it is not please help a newbie here .. please


for pet in d.items():
    pets.append(pet)
print(pets)


also if i use the for loop the second way it still gives me only the last dictionary as answer, the seaman and intellectual one, i am hopeful to learn this lang please help here i have included the second way above please check

CodePudding user response:

What are you trying to achieve? Is this just homework? You can simply create a list of dicts to get the same result:

pets = [{'owner_name': 'holland', 'pet_kind':'mischieveous'},
{'owner_name': 'rowman', 'pet_kind':'smart'},
{'owner_name': 'clark', 'pet_kind':'happy'},
{'owner_name': 'shaun', 'pet_kind':'shy'},
{'owner_name': 'seaman', 'pet_kind':'intellectual'}]

CodePudding user response:

This code would do the work:

d = {
"owner_name": "seaman",
"pet_kind": "intellectual",
}

pets=[pet for pet in d.items()]

print(pets)

the second way you said has a problem that is because you named all your variables "d". they overwrite each other.

CodePudding user response:

  • Firstly, the list comprehension:

    pets = [pets.append(pet) for pet in d.items()]
    

    This syntax collects the results of the method call pets.append(...) in a list, then assigns it to the variable pets. However, pets.append(...) does not return anything — it modifies the list in place — so that will collect a list of None values as returned from each call of the method.

    The method will append it to pets in-place, but then the assignment operator will overwrite pets with the list of None values, which you're seeing.

  • Secondly, the assignments:

    d = {
        'owner_name': 'holland',
        'pet_kind': 'mischievous',
    }
    
    
    d = {
        'owner_name': 'rowman',
        'pet_kind': 'smart',
    }
    
    ...
    

    These will assign different values to the same variable d, just like writing:

    x = 1
    x = 2
    ...
    
  • Finally, the .items() method

    for pet in d.items():
    

    This method is applicable to a dictionary, not a list; it turns that dictionary into a list of pairs, so within the loop, the variable pet will have the value ('owner_name', 'seaman') the first time through the loop and then ('pet_kind', 'intellectual') the second time through the loop.

    As a side-note, it's often more convenient to "unpack" those pairs into a pair of variables, like this:

    for key, value in d.items():
    
  • Related