Reputation: 31
I have 2 different kind of list in python:
1. test1 = ['abc', 'cde', 'fgh']
2. test2 = [{'name': 'me', 'address': 'usa'}]
Both appear to be a list when i check the type:
if test1 is list: --> true
if test2 is list: --> true
is there any way i can check if it is a list of string or list of dictionary?
Upvotes: 0
Views: 90
Reputation: 3
You can use something like the following:
test1 = ["abc", "cde", "fgh"]
if type(test1) is list:
print("Hello")
# Hello
What it will do is use the type(arg)
function where you can pass a variable or data directly to get the type as an output.
For example type("test")
will make it return <type 'str'>
which means it's a string.
Upvotes: 0
Reputation: 38
There are mainly two ways to do this, either with type() or with isinstance(). In case you don't know the exact type of list you have it is preferred to use the former (type()) otherwise the latter is more suitable. My solution addresses the first case and it assumes that the list may contain more than one types.
test1 = ['abc', 'cde', 'fgh']
test2 = [{'name': 'me', 'address': 'usa'}]
test3 = ['abc', 'cde', 3, [2, 1]]
print("Test1 is a list of ", set([type(elem) for elem in test1])) #str
print("Test2 is a list of ", set([type(elem) for elem in test2])) #dict
print("Test3 is a list of ", set([type(elem) for elem in test3])) #str, int, list
I hope this answers your question :-)
Upvotes: 0
Reputation: 79
I would check the first element of each list to determine what kind of list it is. For example:
list1_type = typeof(list1[0])
list2_type = typeof(list2[0])
Of course, a list doesn't have to have all the same type, so without checking every element, you can't know for sure.
Upvotes: 0
Reputation: 78790
Well, test1
and test2
are lists, which you can check via
>>> isinstance(test1, list)
True
>>> isinstance(test2, list)
True
(test1 is list
and test2 is list
actually return False
, because neither are the builtin list
class.)
What might be confusing you is that test2
is a list with only one element, and that element is a dictionary.
>>> test2[0]
{'name': 'me', 'address': 'usa'}
>>> type(test2[0])
dict
If you want to know all the types a list holds, you can do
>>> l = [1, '2', []]
>>> set(map(type, l))
{int, list, str}
# alternative
>>> set(type(x) for x in l)
{int, list, str}
Upvotes: 0
Reputation: 532418
Yes, they are both lists, period; type doesn't take into consideration what is in the list.
You can use type hints to restrict what should be in the list, but these only serve to document the intended use, or by tools like mypy
; they don't affect what you can actually put in the list.
from typing import List, Dict
test1: List[str] = ['abc', 'cde', 'fgh']
test2: List[Dict[str,str]] = [{'name': 'me', 'address': 'use']
def foo(x: List[str]):
...
mypy
will flag as an error any attempt to pass test1
to foo
, but that's allowed when you actually run the code.
mypy
would also flag as an error something like
test3: List[str] = ['foo', 1]
since 1
is not a str
, but again, at runtime that is perfectly legal.
Upvotes: 1
Reputation: 410
Try this snippet:
for item in test1:
print(type(item))
Similar snippet for test2.
Upvotes: 0
Reputation: 5330
To check if an object is of a class you usually use isinstance
>>> isinstance("hello", str)
True
>>> isinstance({}, dict)
True
>>> isinstance([], list)
True
>>> isinstance(5, int)
True
>>> isinstance("hello", int)
False
Upvotes: 0