-2

First example:

my_list = ['apple', 'banana', 'grapes', 'pear']

for c, value in enumerate(my_list, 1):
    print(c, value)

Another example:

[print(int(x)==sum(int(d)**p for p,d in enumerate(x,1)))for x in[input()]]

How does x, d, and p work ?

Community
  • 1
  • 1

3 Answers3

1

So, there are two small questions here:

a)

my_list = ['apple', 'banana', 'grapes', 'pear']

for c, value in enumerate(my_list, 1):
    print(c, value)

Step as follows:

  1. enumerate(my_list, 1) will get a list with index, here the output is a enumereate object, if use list(enumerate(my_list, 1) to have a look, it is [(1, 'apple'), (2, 'banana'), (3, 'grapes'), (4, 'pear')].
  2. So, with every for, the first iterate get c=1, value='apple', the second get c=2, value='banana' ...
  3. Then the final output is:
1 apple
2 banana 
3 grapes 
4 pear

b)

[print(int(x)==sum(int(d)**p for p,d in enumerate(x,1)))for x in[input()]]

Step as follows:

  1. First, it's a list comprehension, I suppose you have known that.
  2. The input first expect a user input, let's input 100 for example, then the input will treat it as a str, so [input()] returns ['100']
  3. Then with for x in [input()] the x is '100'
  4. Next according list comprehension, it will handle int(x)==sum(int(d)**p for p,d in enumerate(x,1))
  5. (int(d)**p for p,d in enumerate(x,1)) will first iterate '100', get something like [(1, '1'), (2, '0'), (3, '0')] if use list to see it, just similar as example 1. Then calculate int(d)**p for every iterate and finally use sum to get the result, similar to int('1')**1 + int('0')**2 + int('0')**3, the result is 1.
  6. So print(int('100')==1 certainly output False
  7. And the return value of print function call is always None, so list comprehension will make the new list is [None].
  8. So the final outout is (NOTE: 100 is the echo of your input):
>>> [print(int(x)==sum(int(d)**p for p,d in enumerate(x,1)))for x in[input()]]
100
False
[None]
atline
  • 28,355
  • 16
  • 77
  • 113
0

It is good practice to look up things in the Python documentation when you have questions about built in functions like enumerate. A link to that is here.

To explain it, enumerate will help you iterate over all values in a list, and if you pass in an optional parameter number (like the 1 in your example), it specifies the starting index to iterate from.

What enumerate returns is first the index and then the item stored at that index in the list. So c in your example is going to be each index (1, 2, 3, etc) as it loops through the for loop. And value in your example is the actual value stored at that index in the list.

Also remember that lists start at index 0 instead of 1 as their first value.

Karl
  • 1,664
  • 2
  • 12
  • 19
0

The common naming system when using enumerate is idx, item it makes it clear what each element represents and would suit you best to follow this naming scheme

l = ['a', 'b', 'c']
print([(idx, item) for idx, item in enumerate(l)])
[(0, 'a'), (1, 'b'), (2, 'c')]

As you can see idx represents the index of the item, and item is the item.

vash_the_stampede
  • 4,590
  • 1
  • 8
  • 20