In Python 3.6, I need to determine which elements from a list contain integers, floats, or strings.

When I use "type" function it returns it's a list element, but what is contained inside that element? a string or a function?

my_list=[3],['XX'],[0],[1],[4],['3']
type(my_list[0])
<class 'list'>

3 Answers

0
Merig On

Your my_list variable is a lists of lists. Try again with

my_list=[3,'XX', 0, 1, 4, '3']
1
vencaslac On

type returns list because what you have is a tuple of lists

my_list=[3],['XX'],[0],[1],[4],['3']

is equivalent to

my_list=([3],['XX'],[0],[1],[4],['3'])

you want to define it as

my_list = [3,'xx',0,1,4,'3']
3
han solo On

You got list because you checked the element at index 0 in the tuple, which is a list

>>> my_list=[3],['XX'],[0],[1],[4],['3'] # my_list is a tuple now :)
>>> [y.__class__ for x in my_list for y in x]
[<type 'int'>, <type 'str'>, <type 'int'>, <type 'int'>, <type 'int'>, <type 'str'>]
>>> [y.__class__.__name__ for x in my_list for y in x]
['int', 'str', 'int', 'int', 'int', 'str']