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
602
votes
42 answers

How to enumerate an enum with String type?

enum Suit: String { case spades = "♠" case hearts = "♥" case diamonds = "♦" case clubs = "♣" } For example, how can I do something like: for suit in Suit { // do something with suit print(suit.rawValue) } Resulting…
Lucien
  • 8,263
  • 4
  • 30
  • 30
344
votes
7 answers

What does enumerate() mean?

What does for row_number, row in enumerate(cursor): do in Python? What does enumerate mean in this context?
user3374098
  • 3,541
  • 3
  • 12
  • 3
172
votes
8 answers

How enumerate all classes with custom class attribute?

Question based on MSDN example. Let's say we have some C# classes with HelpAttribute in standalone desktop application. Is it possible to enumerate all classes with such attribute? Does it make sense to recognize classes this way? Custom attribute…
tomash
  • 12,742
  • 15
  • 64
  • 81
158
votes
11 answers

enumerate() for dictionary in Python

I know we use enumerate for iterating a list but I tried it on a dictionary and it didn't give an error. CODE: enumm = {0: 1, 1: 2, 2: 3, 4: 4, 5: 5, 6: 6, 7: 7} for i, key in enumerate(enumm): print(i, key) OUTPUT: 0 0 1 1 2 2 3 4 4 5 5…
Aditya Krishn
  • 1,997
  • 3
  • 12
  • 8
141
votes
18 answers

Objective-C: Reading a file line by line

What is the appropriate way of dealing with large text files in Objective-C? Let's say I need to read each line separately and want to treat each line as an NSString. What is the most efficient way of doing this? One solution is using the NSString…
Jonathan
113
votes
4 answers

Is enumerate in python lazy?

I'd like to know what happens when I pass the result of a generator function to python's enumerate(). Example: def veryBigHello(): i = 0 while i < 10000000: i += 1 yield "hello" numbered = enumerate(veryBigHello()) for i,…
Adam
  • 2,110
  • 3
  • 14
  • 17
101
votes
5 answers

Find all index position in list based on partial string inside item in list

mylist = ["aa123", "bb2322", "aa354", "cc332", "ab334", "333aa"] I need the index position of all items that contain 'aa'. I'm having trouble combining enumerate() with partial string matching. I'm not even sure if I should be using enumerate. I…
L Shaw
  • 1,067
  • 2
  • 8
  • 7
99
votes
9 answers

Zip or enumerate in R?

What are the R equivalents for these Python list comprehensions: [(i,j) for i,j in zip(index, Values)] [(i,j) for i,j in enumerate(Values)] [(i,j) for i,j in enumerate(range(10,20))] %MWE, indexing or enumerating to …
hhh
  • 50,788
  • 62
  • 179
  • 282
83
votes
6 answers

How can I limit iterations of a loop?

Say I have a list of items, and I want to iterate over the first few of it: items = list(range(10)) # I mean this to represent any kind of iterable. limit = 5 Naive implementation The Python naïf coming from other languages would probably write…
Russia Must Remove Putin
  • 374,368
  • 89
  • 403
  • 331
81
votes
5 answers

Python enumerate() tqdm progress-bar when reading a file?

I can't see the tqdm progress bar when I use this code to iterate my opened file: with open(file_path, 'r') as f: for i, line in enumerate(tqdm(f)): if i >= start and i <= end: print("line #: %s" % i) …
Wei Wu
  • 1,023
  • 1
  • 9
  • 14
76
votes
2 answers

How to iterate `dict` with `enumerate` and unpack the index, key, and value along with iteration

How to iterate a dict with enumerate such that I could unpack the index, key and value at the time of iteration? Something like: for i, (k, v) in enumerate(mydict): # some stuff I want to iterate through the keys and values in a dictionary…
themink
  • 827
  • 1
  • 6
  • 8
57
votes
4 answers

How do I enumerate() over a list of tuples in Python?

I've got some code like this: letters = [('a', 'A'), ('b', 'B')] i = 0 for (lowercase, uppercase) in letters: print "Letter #%d is %s/%s" % (i, lowercase, uppercase) i += 1 I've been told that there's an enumerate() function that can take…
mike
  • 46,876
  • 44
  • 102
  • 112
45
votes
1 answer

Python: Unpacking an inner nested tuple/list while still getting its index number

I am familiar with using enumerate(): >>> seq_flat = ('A', 'B', 'C') >>> for num, entry in enumerate(seq_flat): print num, entry 0 A 1 B 2 C I want to be able to do the same for a nested list: >>> seq_nested = (('A', 'Apple'), ('B',…
Kit
  • 30,365
  • 39
  • 105
  • 149
44
votes
2 answers

Is there a Scala equivalent for the python enumerate?

I'd like the convienience of for i, line in enumerate(open(sys.argv[1])): print i, line when doing the following in Scala for (line <- Source.fromFile(args(0)).getLines()) { println(line) }
Abhinav Sharma
  • 1,145
  • 4
  • 11
  • 12
42
votes
8 answers

Only index needed: enumerate or (x)range?

If I want to use only the index within a loop, should I better use the range/xrange function in combination with len() a = [1,2,3] for i in xrange(len(a)): print i or enumerate? Even if I won't use p at all? for i,p in enumerate(a): …
LarsVegas
  • 6,522
  • 10
  • 43
  • 67
1
2 3
64 65