-1

Need to make lists with the values from another list. These lists must contain only a specific type of value i.e. strings, integers lists, tuples, etc. This is the original list.

x = [55.5,'hello','abc',10,'5','x5',0.25,['A',2,1.5],5,2,5.3,'AEIOU',('dog','cat','chicken'),[1,2,3],1001,['a',1],'world','01/10/2015',20080633,'2.5',0.123,(1,2,'A','B'),5,'chicken',2016,3.1416]

I have tried to use this but to no success:

for i in range(len(x)):
    if x[i].is_integer() == True:
        intlist.append(x[i])
    elif x[i].isalpha() == True:
        strlist.append(x[i])

1 Answers1

1

Those are methods, not functions. The .is_integer() method is one I haven't seen on any object, and .isalpha() is only on strings. You should be checking the type with a different function, say isinstance():

for item in x:
    if isinstance(item, int):
        intlist.append(item)
    elif isinstance(item, str):
        strlist.append(item)

Notice that I also use for item in x: instead of using the indexes.

zondo
  • 19,901
  • 8
  • 44
  • 83