471,310 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

How to justify a container is a list or dictionary?

1
Hi,
Now i raise a question : How to justify a container is a list or dictionary?

thank you for your answer !

Ma Shuai
Oct 10 '07 #1
4 1422
ilikepython
844 Expert 512MB
Hi,
Now i raise a question : How to justify a container is a list or dictionary?

thank you for your answer !

Ma Shuai
I think there is a type function that is built in which gives the type pf the object.
Oct 10 '07 #2
Motoma
3,237 Expert 2GB
I am sure there is a much more elegant way of doing this (there always is), but here I go anyway:

Expand|Select|Wrap|Line Numbers
  1. >>> a = [1, 2, 3, 4, 5]
  2. >>> b = dict()
  3. >>> b['a'] = 1
  4. >>> b['b'] = 2
  5. >>> type(a)
  6. <type 'list'>
  7. >>> type(b) == type([])
  8. False
  9. >>> type(b) == type(dict())
  10. True
  11. >>> 
  12.  
Oct 10 '07 #3
bvdet
2,851 Expert Mod 2GB
Here is another way using isinstance():
Expand|Select|Wrap|Line Numbers
  1. >>> seq = [1,2,3,4,5]
  2. >>> dd = dict(zip(seq, ['a','b','b','d','e']))
  3. >>> isinstance(seq, list)
  4. True
  5. >>> isinstance(dd, dict)
  6. True
  7. >>> isinstance(seq, dict)
  8. False
  9. >>> 
Oct 10 '07 #4
Motoma
3,237 Expert 2GB
Here is another way using isinstance():
Expand|Select|Wrap|Line Numbers
  1. >>> seq = [1,2,3,4,5]
  2. >>> dd = dict(zip(seq, ['a','b','b','d','e']))
  3. >>> isinstance(seq, list)
  4. True
  5. >>> isinstance(dd, dict)
  6. True
  7. >>> isinstance(seq, dict)
  8. False
  9. >>> 
Nice!
See, that is why I participate in threads like this, I get to learn neat little tricks :D
Oct 10 '07 #5

Post your reply

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

Similar topics

5 posts views Thread by The Plankmeister | last post: by
3 posts views Thread by pecan | last post: by
3 posts views Thread by erik gartz | last post: by
3 posts views Thread by Cristiano Paris | last post: by
7 posts views Thread by Andrew Robinson | last post: by
4 posts views Thread by =?Utf-8?B?SnVlcmdlbiBELg==?= | last post: by
5 posts views Thread by tshad | last post: by
12 posts views Thread by =?Utf-8?B?THVpZ2k=?= | last post: by
reply views Thread by rosydwin | 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.