Questions tagged [enumerate]

Related to functions or methods that return enumerations or enumerated-types. These can be objects, data types or data structures in which each of the items of a set has a one-to-one correspondence with a set of ordered numbers. An example is the `enumerate()` built-in function in Python.

Related to functions or methods that return enumerations or enumerated-types. These can be objects, data types or data structures in which each of the items of a set has a one-to-one correspondence with a set of ordered numbers. An example is the enumerate() built-in function in Python.

In Python, the function enumerate(sequence, start=0) sequence must be an object which supports iteration. enumerate() returns an enumerate object which consists of tuples containing a count (from start) and the values obtained from iterating over sequence (from the Python Standard Library official documentation)

975 questions
38
votes
2 answers

Does Kotlin have an "enumerate" function like Python?

In Python I can write: for i, element in enumerate(my_list): print(i) # the index, starting from 0 print(element) # the list-element How can I write this in Kotlin?
fafl
  • 7,222
  • 3
  • 27
  • 50
34
votes
5 answers

(Python) Counting lines in a huge (>10GB) file as fast as possible

I have a really simple script right now that counts lines in a text file using enumerate(): i = 0 f = open("C:/Users/guest/Desktop/file.log", "r") for i, line in enumerate(f): pass print i + 1 f.close() This takes around 3 and a half minutes…
Adrienne
  • 465
  • 1
  • 4
  • 6
30
votes
2 answers

Enumerate Dictionary Iterating Key and Value

I have a dictionary called regionspointcount that holds region names (str) as the keys and a count of a type of feature within that region (int) as the values e.g. {'Highland':21}. I am wanting to iterate the key and value of dictionary while…
Worm
  • 1,313
  • 2
  • 11
  • 28
27
votes
4 answers

Python's enumerate in Ruby?

def enumerate(arr): (0..arr.length - 1).to_a.zip(arr) Is something built in for this? It doesn't need to have it's members immutable, it just needs to be in the standard library. I don't want to be the guy who subclasses the Array class to add…
yurisich
  • 6,991
  • 7
  • 42
  • 63
25
votes
10 answers

Python enumerate reverse index only

I am trying to reverse the index given by enumerate whilst retaining the original order of the list being enumerated. Assume I have the following: >> range(5) [0, 1, 2, 3, 4] If I enumerate this I would get the following: >>…
rozzy
  • 2,828
  • 6
  • 25
  • 35
24
votes
6 answers

Python enumerate downwards or with a custom step

How to make Python's enumerate function to enumerate from bigger numbers to lesser (descending order, decrement, count down)? Or in general, how to use different step increment/decrement in enumerate? For example, such function, applied to list…
Mirzhan Irkegulov
  • 17,660
  • 12
  • 105
  • 166
23
votes
8 answers

How to make a continuous alphabetic list python (from a-z then from aa, ab, ac etc)

I would like to make a alphabetical list for an application similar to an excel worksheet. A user would input number of cells and I would like to generate list. For example a user needs 54 cells. Then I would…
Seb
  • 3,655
  • 3
  • 17
  • 17
22
votes
2 answers

Latex - skip numbering in an enumerate environment

I would like to skip numbering in a Latex enumerate environment to generate lists as follows: (1) Item 1.. (2) Item 2.. (5) Item 5.. (6) Item 6.. and so on. The Latex code is auto-generated, so ideally, I would like to insert "silent" \item-s in the…
ARV
  • 6,287
  • 11
  • 31
  • 41
19
votes
6 answers

Python strange behavior with enumerate

I know I'm not supposed to modify the list inside a loop, but just out of curiosity, I would like to know why the number of iterations is different between the following two examples. Example 1: x = [1, 2, 3, 4, 5] for i, s in enumerate(x): del…
dbdq
  • 355
  • 2
  • 8
15
votes
1 answer

What is the most pythonic way to have inverse enumerate of a list?

The first thing that comes to mind is: >>> l = list('abcdef') >>> for i in range(len(l)-1, -1, -1): ... item = l[i] ... print(i, item) ... 5 f 4 e 3 d 2 c 1 b 0 a I tried using the following: >>> l ['a', 'b', 'c', 'd', 'e', 'f'] >>> for i,ch in…
Iliyan Bobev
  • 3,070
  • 2
  • 20
  • 24
15
votes
3 answers

Complexity of enumerate

I see a lot of questions about the run-time complexity of python's built in methods, and there are a lot of answers for a lot of the methods (e.g. https://wiki.python.org/moin/TimeComplexity ,…
10'004
  • 163
  • 1
  • 1
  • 10
14
votes
4 answers

Easiest way to include a stop parameter in enumerate python?

Ss there a simple way to iterate over an iterable object that allows the specification of an end point, say -1, as well as the start point in enumerate. e.g. for i, row in enumerate(myiterable, start=2): # will start indexing at 2 So if my object…
Tom Smith
  • 371
  • 2
  • 3
  • 14
14
votes
1 answer

Python enumerate through 2D array in numpy

I want a function that behaves like enumerate, but on numpy arrays. >>> list(enumerate("hello")) [(0, "h"), (1, "e"), (2, "l"), (3, "l"), (4, "o")] >>> for x, y, element in enumerate2(numpy.array([[i for i in "egg"] for j in range(3)])): …
rlms
  • 10,650
  • 8
  • 44
  • 61
12
votes
1 answer

VB 6 How to make Custom Collection Class to support For Each

I've been placed on a project whose client front end is written in VB 6, ack! I'm trying to develop a custom collection class that supports the For...Each syntax. Is this possible in VB 6? Or am I stuck with using the For..Next with counter to…
Chris
  • 123
  • 1
  • 5
12
votes
1 answer

Python - Go through list without last element

I have a list of tuples and want to create a new list. The elements of the new list are calculated with the last element of the new list (first element is 0) and the the second element of the next tuple of the old list. To understand…
mark0004
  • 157
  • 1
  • 1
  • 6
1
2
3
64 65