How to find an integer equivalent of infinity as an index above all others?

I am trying to work a list in Python. I want to insert new values, but at the extreme end of the list.

insert() works OK generally, but it takes two parameters: insert(0, 10) adds 10 to the left end of the list, as 0 is lower than (or equal to) the zeroth value in the list. so, if I start with a=[1,2,3,4,5],

z=a.insert(0, 10)

the new list looks like [10,1,2,3,4,5], which is as expected.

The key question is how to do this and insert at the end of the list. so, for example,this will work:

z = a.insert(100, 20)

gives:

z = [1,2,3,4,5,20]

since 100 is larger than the number of items in the list, thus placing it at the end. But, of course, that will fail to work right if there are more than 100 items in the list.

The logical choice is to define infinity, then use this code to add to the right-end, regardless of array size:

z = a.insert(infinity, 30)

Since I don't know the array size before starting, I am in a pickle. I have tried (1) using math.inf, as well as (2) using float("inf"). The first fails because "expected an integer but got a float", the second fails similarly. Trying to 'cast' it - i.e., top_index = int(float("inf")) - does not give an integer rep of positive infinity.

I could work around this by saying a.insert(len(a)+1, 30),

...but this seems hokey and requires additional steps (and tolls on speed).

How else can you insert above the highest indexed value?



from Recent Questions - Stack Overflow https://ift.tt/3zrI6fa
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation