Login or Sign Up
Logging in...
Remember me
Log in
Or
Sign Up
Forgot password or user name?
Log in with
Search in titles only
Search in Python only
Search
Advanced Search
Forums
Product Launch
Updates
Today's Posts
Member List
Calendar
Home
Forum
Topic
Python
How to justify a container is a list or dictionary?
Collapse
X
Collapse
Posts
Latest Activity
Photos
Page
of
1
Filter
Time
All Time
Today
Last Week
Last Month
Show
All
Discussions only
Photos only
Videos only
Links only
Polls only
Events only
Filtered by:
Clear All
new posts
Previous
template
Next
lv191
New Member
Join Date:
Oct 2007
Posts:
1
#1
How to justify a container is a list or dictionary?
Oct 10 '07, 05:42 AM
Hi,
Now i raise a question : How to justify a container is a list or dictionary?
thank you for your answer !
Ma Shuai
ilikepython
Recognized Expert
Contributor
Join Date:
Feb 2007
Posts:
844
#2
Oct 10 '07, 10:22 AM
Originally posted by
lv191
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.
Comment
Post
Cancel
Motoma
Recognized Expert
Specialist
Join Date:
Jan 2007
Posts:
3236
#3
Oct 10 '07, 01:57 PM
I am sure there is a much more elegant way of doing this (there always is), but here I go anyway:
[CODE=python]
>>> a = [1, 2, 3, 4, 5]
>>> b = dict()
>>> b['a'] = 1
>>> b['b'] = 2
>>> type(a)
<type 'list'>
>>> type(b) == type([])
False
>>> type(b) == type(dict())
True
>>>
[/CODE]
Comment
Post
Cancel
bvdet
Recognized Expert
Specialist
Join Date:
Oct 2006
Posts:
2851
#4
Oct 10 '07, 02:41 PM
Here is another way using
isinstance()
:[code=Python]>>> seq = [1,2,3,4,5]
>>> dd = dict(zip(seq, ['a','b','b','d' ,'e']))
>>> isinstance(seq, list)
True
>>> isinstance(dd, dict)
True
>>> isinstance(seq, dict)
False
>>> [/code]
Comment
Post
Cancel
Motoma
Recognized Expert
Specialist
Join Date:
Jan 2007
Posts:
3236
#5
Oct 10 '07, 03:23 PM
Originally posted by
bvdet
Here is another way using
isinstance()
:[code=Python]>>> seq = [1,2,3,4,5]
>>> dd = dict(zip(seq, ['a','b','b','d' ,'e']))
>>> isinstance(seq, list)
True
>>> isinstance(dd, dict)
True
>>> isinstance(seq, dict)
False
>>> [/code]
Nice!
See, that is why I participate in threads like this, I get to learn neat little tricks :D
Comment
Post
Cancel
Previous
template
Next
Working...
Yes
No
OK
OK
Cancel
👍
👎
☕
Comment