Linked Questions
23 questions linked to/from Cleanest way to get last item from Python iterator
2
votes
4
answers
705
views
Get only the last element of an iterator in the most Pythonic way [duplicate]
How do I get the last element of an iterator? I'm aware that I have to exhaust the iterator and return the last value it produced, so the plain approach would be:
def last(it):
for value in it:
...
2
votes
1
answer
991
views
How to get the position of the last match of a regex? [duplicate]
I have a regex that matches numbers and I want to get the position of the last-matching number.
This is what I got right now:
def find_last_match_pos(pattern, s):
match = None
for match in ...
221
votes
36
answers
207k
views
Get last n lines of a file, similar to tail
I'm writing a log file viewer for a web application and I want to paginate through the lines of the log file. The items in the file are line based with the newest item at the bottom.
So I need a tail(...
280
votes
8
answers
159k
views
Scoping in Python 'for' loops
I'm not asking about Python's scoping rules; I understand generally how scoping works in Python for loops. My question is why the design decisions were made in this way. For example (no pun intended):
...
129
votes
3
answers
142k
views
Getting only element from a single-element list in Python?
When a Python list is known to always contain a single item, is there a way to access it other than:
mylist[0]
You may ask, 'Why would you want to?'. Curiosity alone. There seems to be an alternative ...
25
votes
14
answers
6k
views
Stopping a Reduce() operation mid way. Functional way of doing partial running sum
I have been doing some functional programming and had a question. Perhaps I might be missing something but is there any way to stop a "reduce()" function midway? Lets say when I reach a certain ...
25
votes
5
answers
12k
views
Does python officially support reusing a loop-variable after the loop?
Is the following code bad practice?
for i in some_values:
do_whatever(i)
do_more_things(i)
Somehow, it feels to me like the variable i should remain in the scope to the block inside the for-loop. ...
9
votes
10
answers
68k
views
How to check if end of list was reached?
If have a list, say a=[1,2,3], and I want to see if a[4] is null, is there a way to do that, without using an exception or assertion?
10
votes
4
answers
15k
views
Fastest way to get the first and last element of a python iterator
I'm trying to perform checks on the first and last elements of an interator. It has several thousand entries, so I need an expeditious method of checking. If found this post, that put me onto this ...
4
votes
4
answers
44k
views
Find the last row from a CSV input Python
I didn't really find an example related to my question as I don't know Pandas so I post it here. Let me know if this is not clear or already have been responded.
I have a CSV input which I import ...
7
votes
1
answer
5k
views
Why doesn't Python delete the iterate variable after a loop? [duplicate]
I found the situation when running ipython. The version of python is 2.6.6 and ipython 0.13. For example:
In [1]: for i in range(100):
...: pass
...:
In [2]: who
Out [2]: i
In [3]: ...
0
votes
4
answers
8k
views
Last element in a python iterator [duplicate]
I want to iterate through the lines of a file, and print some output for each of them. All lines printed should have a ,\n at the end, except for the last line.
My first approach was to use look for ...
3
votes
5
answers
476
views
Filter text which appears between two marks
Part 1
What is the easiest way to create a text filter which outputs only text surrounded by two predefined marks. I don't mind using any standard tool: sed, awk, python, ...
For example, i would ...
2
votes
1
answer
2k
views
reduce large dataset in python using reduce function
I was trying a simple reduce function on a dataset of 1000000 integers,
Here is what I'm trying to do,
from functools import reduce
a = (x for x in range(1,1000000))
reduce(lambda x,y: x*y,a)
Problem ...
-1
votes
1
answer
2k
views
How to Get Last Item in a Python Generator
Question: how can I get the last item in a python generator in a fast and memory-efficient way?
MWE:
import snscrape.modules.twitter as sntwitter
import time; start = time.time()
query = "Rooty ...