471,354 Members | 2,062 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

python dictionaries

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
1 1126
bvdet
2,851 Expert Mod 2GB
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.

Similar topics

reply views Thread by Till Plewe | last post: by
44 posts views Thread by Iwan van der Kleyn | last post: by
4 posts views Thread by Tim Henderson | last post: by
1 post views Thread by François Pinard | last post: by
reply views Thread by Kurt B. Kaiser | last post: by
reply views Thread by Kurt B. Kaiser | last post: by
5 posts views Thread by Larry Bates | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.