Changing the id field to an _id field for the lowest tweet id I am collecting tweets using a Python script and I want to store them in a MongoDB database. 

I want to find out the lowest tweet id and for each tweet, I want to change the id field to an _id field.

Is it possible not having to iterate over all the tweets with a for loop in order to do these 2 things.

search_result = #search result from Twitter API

min_id = float('inf')

result = []
for tweet in search_result:
    if tweet['id'] &lt; min_id:
        min_id = tweet['id']
    tweet['_id'] = tweet.pop('id')
    result.append(tweet)
Three parts to your questions: 1) find lowest tweet ID and 2) assign id: to _id 3) can (1) and (2) be done without iterating over the list.

To answer (3) first, (1) definitely can be done without iteration, but I am unaware of how to do (2) without iteration.

(1) Lowest Tweet ID
The sort function is great for this. This question has more on sorting. To create a new list with the lowest id in the first position:

results = sorted(search_results, key=lambda x: x.id)


To do the same thing replacing the list:

search_results.sort(search_results, key=lambda x: x.id)


Simply access the "0"th result in the list and you have your lowest tweet id.

2) Assign id: to _id

Perhaps someone can answer this better, but the best way I know of is how you've done it:

for tweet in search_results:
    tweet['_id'] = tweet.pop('id')


Your final code would look something like:

search_result = #search result from Twitter API
results = sorted(search_results, key=lambda x: x.id)
min_id = results[0]['id']
for tweet in search_results:
    tweet['_id'] = tweet.pop('id')