Ashish Prajapati
Ashish Prajapati

Reputation: 36

Python indexing integer and float

I have a doubt as suppose i have a list mixed list of elements as

Pylist = [1,2,3,'a','b','c',2.0,2.2]

now if i retrieve the index value of 1 and 6 it provides me as

>>> Pylist[1]
2
>>> Pylist[6]
2.0

and if i check the type they return int and float respectively but when i use index method on it, will return me the same index position after which there type is different, index place and value also, can anyone explain

>>> Pylist.index(2)
1
>>> Pylist.index(2.0)
1
>>>

???

Upvotes: 0

Views: 3187

Answers (3)

7koFnMiP
7koFnMiP

Reputation: 477

You can use this to return the index of each 2/2.0:

for i in set(Pylist):
    if Pylist.count(i) > 1:
        print(" ".join([str(index) for index, value in enumerate(Pylist) if value == el]))

Upvotes: 1

dimay
dimay

Reputation: 2804

If you want to get index 2.0 value you can use isinstance(Pylist[i], float):

[i for i,k in enumerate(Pylist) if isinstance(Pylist[i], float) and Pylist[i] == 2.0] # [6]

Upvotes: 1

rozumir
rozumir

Reputation: 905

It's because 2 == 2.0 is True and index() method finds first value that meets this condition, searching throught the list.

Upvotes: 2

Related Questions