Question: i have a file with information in the pattern of:

```
.343423 1
.434322 1
.453434 1
.534342 1

```

Equal size each line and row in sorted order..I have a variable "a" with a value and need to get the row number which it closest to "a" comparing with the values in the 1st column..

Till now i was coping the 1st column element into list and then using bisect method i am getting the row\_num...but as i need to perform this many times..this has become painfully slow as i need to copy some 4000 element to list everytime..

so now i am thinking of doing it with dict instead of datastructure as i would be faster...but i dont know whether we can use dict in bisect if possible how can we use for this case please suggest...
if not possible are they any method for loading data into list faster then normal???
thanking you...

Answer: 
Similar to Dave Kirby's solution, consider the [sortedcontainers](http://www.grantjenks.com/docs/sortedcontainers/) module on [PyPI](https://pypi.python.org/pypi/sortedcontainers). It's pure-Python, [fast](http://www.grantjenks.com/docs/sortedcontainers/performance.html), and provides a [SortedDict type with bisect](http://www.grantjenks.com/docs/sortedcontainers/sorteddict.html#sortedcontainers.SortedDict.d.bisect_left) over the keys. It's also a lot faster than a balanced binary tree type for bulk-loading data from a file.

In your case, something like this might work:

```
from sortedcontainers import SortedDict
with open('data.txt') as fptr:
    sd = SortedDict(map(int, line[1:].split()) for line in fptr)

# sd now contains key, value pairs corresponding to the columns in your data file
# Lookup index of desired key:

pos = sd.bisect(434323)

# pos points to the index of the key 434322
# get that key:

key = sd.iloc[pos]

# now get the value:

value = sd[key]

```

The operations: bisecting, indexing, and key lookup are all very fast within the sortedcontainers module. This solution requires that you can keep the entire contents of the file in memory.
I don't get why you need to copy the elements. This is the slow part. Can't you load the list one time on startup, and then always use the same list ?

A dict will be slower than a list anyway (and I belive [not sure] it is implemented as a hash\_map, thus there is no order, thus you can't use bisect).