Idiomatic Python to convert list to dict

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • James Fassett

    Idiomatic Python to convert list to dict

    Hi all,

    Simple question really on a best practice. I want to avoid adding
    duplicates to a list.

    my_list = ['a', 'b', 'c', 'd', 'e']
    dup_map = {}
    for item in my_list:
    dup_map[item] = True

    # ... sometime later

    for complex_dict in large_list:
    if complex_dict["char"] not in dup_map:
    my_list.append( complex_dict["char"])
    dup_map[complex_dict["char"]] = True

    For the first part (generating the duplicate map) is there a more
    idiomatic Python method for flipping the list into a dict?

    Is there a better way to achieve the overall objective (as hinted at
    by the above algorithm)?

    Thanks in advance for any tips.

    James.
  • Diez B. Roggisch

    #2
    Re: Idiomatic Python to convert list to dict

    James Fassett schrieb:
    Hi all,
    >
    Simple question really on a best practice. I want to avoid adding
    duplicates to a list.
    >
    my_list = ['a', 'b', 'c', 'd', 'e']
    dup_map = {}
    for item in my_list:
    dup_map[item] = True
    >
    # ... sometime later
    >
    for complex_dict in large_list:
    if complex_dict["char"] not in dup_map:
    my_list.append( complex_dict["char"])
    dup_map[complex_dict["char"]] = True
    >
    For the first part (generating the duplicate map) is there a more
    idiomatic Python method for flipping the list into a dict?
    >
    Is there a better way to achieve the overall objective (as hinted at
    by the above algorithm)?
    >
    Thanks in advance for any tips.
    Instead of a dict, use a set. It's immediatly contructable from my_list,
    and better suited for the task anyway.

    Diez

    Comment

    • craig75

      #3
      Re: Idiomatic Python to convert list to dict

      On Jul 10, 10:06 am, James Fassett <ja...@reggieba nd.comwrote:
      Hi all,
      >
      Simple question really on a best practice. I want to avoid adding
      duplicates to a list.
      >
      my_list = ['a', 'b', 'c', 'd', 'e']
      dup_map = {}
      for item in my_list:
          dup_map[item] = True
      >
      # ... sometime later
      >
      for complex_dict in large_list:
          if complex_dict["char"] not in dup_map:
              my_list.append( complex_dict["char"])
              dup_map[complex_dict["char"]] = True
      >
      For the first part (generating the duplicate map) is there a more
      idiomatic Python method for flipping the list into a dict?
      >
      Is there a better way to achieve the overall objective (as hinted at
      by the above algorithm)?
      >
      Thanks in advance for any tips.
      >
      James.
      The dictionary seems like overkill here because, if I understand
      correctly, the only value associated with a key is "True". So in that
      case just remove all the code related to the dictionary (and
      complex_dict) and you end up with

      my_list = ['a', 'b', 'c', 'd', 'e']

      # ... sometime later
      for char in large_list:
      if char not in my_list:
      my_list.append( char)


      However, as Diez suggests, use a set:

      my_list = set(['a', 'b', 'c', 'd', 'e'])
      # ... sometime later
      for char in large_list:
      my_list.add(cha r) # will not add duplicates

      Comment

      • James Fassett

        #4
        Re: Idiomatic Python to convert list to dict

        On Jul 10, 6:13 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
        my_list = ['a', 'b', 'c', 'd', 'e']
        dup_map = {}
        for item in my_list:
            dup_map[item] = True
        >
        # ... sometime later
        >
        for complex_dict in large_list:
            if complex_dict["char"] not in dup_map:
                my_list.append( complex_dict["char"])
                dup_map[complex_dict["char"]] = True
        >
        Instead of a dict, use a set. It's immediatly contructable from my_list,
        and better suited for the task anyway.
        Cheers,

        I rewrote it similar to:

        dup_map = set(['a', 'b', 'c', 'd', 'e'])

        # ... sometime later

        for complex_dict in large_list:
        dup_map.add(com plex_dict["char"])

        my_list = list(dup_map)

        That is a little nicer. Thanks again,
        James.

        Comment

        Working...