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

python dictionaries

P: 54
If i have 2 lists/strings, and I want them to be coupled together in a dictionary, what's the best way going about this...

the method im using only inclues the last item in the list.

Expand|Select|Wrap|Line Numbers
  1.  
  2. >>> name = ['a', 'b', 'c']
  3. >>> food = ['apple', 'banana', 'carrot']
  4. >>> while count < b:
  5. ...     dict = {name[count] : food[count]}
  6. ...     count += 1
  7. ... 
  8. >>> 
  9. >>> dict
  10. {'c': 'carrot'}
  11.  
Aug 14 '07 #1
Share this Question
Share on Google+
1 Reply


bvdet
Expert Mod 2.5K+
P: 2,851
If i have 2 lists/strings, and I want them to be coupled together in a dictionary, what's the best way going about this...

the method im using only inclues the last item in the list.

Expand|Select|Wrap|Line Numbers
  1.  
  2. >>> name = ['a', 'b', 'c']
  3. >>> food = ['apple', 'banana', 'carrot']
  4. >>> while count < b:
  5. ...     dict = {name[count] : food[count]}
  6. ...     count += 1
  7. ... 
  8. >>> 
  9. >>> dict
  10. {'c': 'carrot'}
  11.  
You have dict on the wrong side of the equal sign.
Expand|Select|Wrap|Line Numbers
  1. >>> name = ['a', 'b', 'c']
  2. >>> food = ['apple', 'banana', 'carrot']
  3. >>> dd = dict(zip(name, food))
  4. >>> dd
  5. {'a': 'apple', 'c': 'carrot', 'b': 'banana'}
  6. >>> 
Aug 14 '07 #2

Post your reply

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