0

I'm new to programming and I'm making a simple program to test. Here is the code:

list1 = [1,2,d,,t,h,7,8]

for x in list1:
    if x == 

I'm trying to iterate in my list and check to see which item is a string and which is a number(I know its basic, but im new). So what would be a correct way to write this line of code. All suggestions would help

6 Answers 6

2

In Python, use the builtin isinstance(variable, type) to test whether a variable is of a given type.

The type variable can also be a tuple of several types to test against.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much. That fixed my program's problem
1

Your list is a little messed up. If those letters are supposed to be strings, it should look like this:

list1 = [1,2,'d','t','h',7,8]

Otherwise they are referring to variables.

To check if number

for x in list1:
    if isinstance(x, int):
        ...

To check if string

for x in list1:
    if isinstance(x, str):
       ...

Combination

for x in list1:
    if x.isinstance(x, int):
        ....
    elif isinstance(x, str)L
       ....

2 Comments

isdigit is a string function to detect if all characters in the string are numeric. In the OP's sample, the integer values are not strings, but actual integers.
@PaulMcGuire fixed that. he didn't have quotations around the letters either so I didn't know if maybe some would have quotes around them.
1

This should print the type of each element in your list

for item in list1:
    print type(item)

Comments

0

Assuming your list contains purely numbers and strings:

for x in list1:
    if type(x) == str:
        # do something
    else:
        # do something for numbers

1 Comment

isinstance is generally preferred practice over type(x)=='. Given an object that is an instance of class B which is a subclass of A, type(obj)==A` will evaluate as False, but isinstance(obj,A) will return True.
0

Since everyone is giving answers similar to my old one (see below), I'll try something else.You can easily make 2 lists that separate ints and strings with Python's list comprehensions:

strings = [x for x in list1 if isinstance(x, str)]
intlist = [x for x in list1 if isinstance(x, int)]

Using list comprehensions made the code more compact and easier to understand.

Old response:

list1 = [1, 4, 5, '5', '3', 3, 'a']
for x in list1:
    if(isinstance(x, str)):
        print(str(x) + ' is a string.')
    else:
        print(str(x) + ' is not a string.')

Comments

-1

You could try this one,

list1=[1,2,'d','t','h',7,8]

for data in list1:
    if type(data) is str:
        #do something with it
    elif type(data) is int:
        #do something with it

you can try something like this or any of those methods mentioned from other answers in the python interpreter to see it works -

>> type(1) is str

and you would get

>> False

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.