0

I am trying to add keys and value to a python dictionary in a loop. I am quite new to python and I am not able to get it right.

This is my piece of code, where i want to loop through and build the dictionary in a loop with the key being the album name and value being the song list.

    for alb in l.songs:
       if alb.album not in song_database:
           song_database[alb.album] = alb.name

had it been perl i would have done something like this, i am just assuming the kys and values are coming from two different arrays.

    foeach(@album_name){
    my $key = $_;
      foreach(@song_name){
        push (@{song_hash{"key"}},$_);
      }
    }

I would like to know how to do it in python ?

5
  • in my dictionary the key is alb.album and not the value of the object. Commented Nov 19, 2013 at 22:45
  • Your perl code doesn't have any equivalent of that if clause; it just replace any existing value for the key with the new value. So why did you add the if in Python? Commented Nov 19, 2013 at 22:45
  • Ok, I think now I get it: alb is not an album, but a song. See Matthew's or Óscar's answer then. Commented Nov 19, 2013 at 22:48
  • Also, your perl code is using the string "key_1" in place of $key, not getting song_name from anywhere, and using an explicit loop to copy an array instead of just copying it, which… is silly, and would be just as silly once translated to Python. Can you give us actual working perl code, if you can't give us a clear explanation in English? Commented Nov 19, 2013 at 22:52
  • its actually key, i made a typo! Commented Nov 19, 2013 at 22:53

5 Answers 5

4

You need to append to an array in python, like you do in perl. Since python does not autovivify you also need to create the array:

for alb in l.songs:
   if alb.album not in song_database:
       song_database[alb.album] = [alb.name]
   else:
       song_database[alb.album].append(alb.name)
Sign up to request clarification or add additional context in comments.

3 Comments

no, alb is just an object of the songs, which has attributes of name, ablum etc
@ArunRaman That's exactly what I'm saying. It's not an album, it's a song. That's where the whole confusion came from. And that's why song_database[alb.album] = alb.name reads like it does one thing, but does another.
@LukasGraf Ya, sorry its a newbie question.
2

A shorter and more pythonic solution would be to use a defaultdict:

from collections import defaultdict
song_database = defaultdict(list)

for song in l.songs:
    song_database[song.album].append(song.name)

Comments

0

I think you just forgot to add an indention? Like this:

for alb in l.songs:
    if alb.album not in song_database:
        song_database[alb.album] = alb.name

Comments

0

i want to loop through and build the dictionary in a loop with the key being the album name and value being the song list.

It looks like your code is making alb.album the dictionary key, and alb.name the dictionary value. Try re-arraging:

for alb in l.songs:
    if alb.name not in song_database:
        song_database[alb.name] = alb.album

Comments

0

If based on your answer to the question above, you want to append if the album is already in the database, try this way:

for alb in l.songs:
    if alb.album not in song_database:
        song_database[alb.album] = [alb.name]
    else:
        song_database[alb.album] += [alb.name]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.