0

If I have a list as below:

samplist= [3,4,5,'abc']

How can I find out whether which index is a str or Int.I want to concatenate a string s='def' with the string 'abc' which is available in the list.

Assume that I am not aware I don't know anything about the string, neither the name nor its index in list. All i know is there is a string in the list samplist and I want to loop through and find it out so that it can be concatenated with string s='def'/.

user3369157
  • 137
  • 1
  • 9

2 Answers2

2
for i in xrange(len(samplist)):
    if isinstace(samplist[i], str):
        samplist[i] += 'def'
Jayanth Koushik
  • 9,476
  • 1
  • 44
  • 52
0

type() command also works:

>>> list = [1, 2, 'abc']
>>> type(list)
<class 'list'>
>>> type(list[1])
<class 'int'>
>>> type(list[2])
<class 'str'>
>>> type(list[2]) is str
True
>>> type(list[1]) is str
False
Abhishek Bansal
  • 12,589
  • 4
  • 31
  • 46