By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,907 Members | 1,963 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,907 IT Pros & Developers. It's quick & easy.

Generating a list of tuples

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

Expand|Select|Wrap|Line Numbers
  1. names=['george', 'paul', 'john', 'ringo']
and a location string:

Expand|Select|Wrap|Line Numbers
  1. location='liverpool'
Now you want to combine these into a list of tuples based on location:

[('george','liverpool'), ('paul','liverpool'), ('john','liverpool'), ('ringo','liverpool')]

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?
Apr 15 '09 #1
Share this Question
Share on Google+
1 Reply

Expert Mod 2.5K+
P: 2,851
Following are two ways. You can decide how classy they are.
Expand|Select|Wrap|Line Numbers
  1. def zip_location(seq, location):
  2.     return [(item,location) for item in seq]
  4. def zip_location(seq, location):
  5.     return zip(seq, [location for i in range(len(seq))])
Apr 15 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.