Generating a list of tuples

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Benny the Guard
    New Member
    • Jun 2007
    • 92

    Generating a list of tuples

    Looking for the classiest way to do this, just for fun. Say you have a list of names

    Code:
    names=['george', 'paul', 'john', 'ringo']
    and a location string:

    Code:
    location='liverpool'
    Now you want to combine these into a list of tuples based on location:

    [('george','live rpool'), ('paul','liverp ool'), ('john','liverp ool'), ('ringo','liver pool')]

    Kind of like a zip function but with all values from names present and kind of like a map function but with the location used not None. So a little twist on some common functions. Thoughts?
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Following are two ways. You can decide how classy they are.
    Code:
    def zip_location(seq, location):
        return [(item,location) for item in seq]
    
    def zip_location(seq, location):
        return zip(seq, [location for i in range(len(seq))])

    Comment

    Working...