Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print types within a list

Tags:

python

types

list

So I was given a list and I must print the type of each item in the list. I can clearly see that there are strings and integers but I need it to print out in Python. We just learned for loops so I feel like that is what they are looking for but I cannot get it to print out.

like image 679
RPmich Avatar asked Jan 27 '16 03:01

RPmich


2 Answers

ls = [type(item) for item in list_of_items]
print(ls)
like image 187
Ashish Kumar Avatar answered Oct 14 '22 04:10

Ashish Kumar


Essentially, the type function takes an object and returns the type of it. Try the below code:

for item in [1,2,3, 'string', None]:
    print type(item)

Output:

<type 'int'>
<type 'int'>
<type 'int'>
<type 'str'>
<type 'NoneType'>
like image 23
Rudrani Angira Avatar answered Oct 14 '22 04:10

Rudrani Angira