#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    19
    Rep Power
    0

    Using a dictionary to replace values in a list


    I have the following dictionary:

    Code:
    months = {'01': 'Jan', '02': 'Feb', '03': 'Mar', '04': 'Apr',
            '05': 'May', '06': 'Jun', '07': 'Jul', '08': 'Aug', '09': 'Sep',
            '10': 'Oct', '11': 'Nov', '12': 'Dec'}
    and I have the following list:

    Code:
    month = ['02', '05', '06']
    Basically, I need to run through the list "month" and if a value in the list matches the key in the dictionary, the dictionaries' key's value needs to replace the value in the list...

    So, the list should look like this in the end:

    Code:
    month = ['Feb', 'May', 'Jun']
    I'm having trouble coming up with a loop that will do this.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    436
    Rep Power
    67
    Originally Posted by cspctec
    I'm having trouble coming up with a loop that will do this.
    I wonder why; this is almost as basic as it gets. Using list comprehension:

    Code:
    month = [months[m] for m in month if m in months]
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (work laptop), Trisquel GNU/Linux 6.0.1 (mini laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    19
    Rep Power
    0
    Originally Posted by SuperOscar
    I wonder why; this is almost as basic as it gets. Using list comprehension:

    Code:
    month = [months[m] for m in month if m in months]
    I've never used dictionaries before, wise guy. Thanks for the help anyways.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    Joensuu, Finland
    Posts
    436
    Rep Power
    67
    Originally Posted by cspctec
    I've never used dictionaries before, wise guy.
    Well I’m sorry, didn’t mean to mock; I only meant that Python’s for loop is extremely versatile and can be used in any sequence, even those you haven’t previously encountered. “for key in dict” iterates through all the keys in the dictionary; “for key, value in dict.items()” can be used to get keys and their corresponding values at the same time.
    My armada: openSUSE 13.1 (home desktop, home laptop), Crunchbang Linux 11 (work laptop), Trisquel GNU/Linux 6.0.1 (mini laptop), Ubuntu 14.04 LTS (server), Android 4.2.1 (tablet), Windows 7 Ultimate (testbed)
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    1
    Rep Power
    0

    Wow!


    Wait until I start asking my really rookie questions!

IMN logo majestic logo threadwatch logo seochat tools logo