Python

jassim Al-Hatem
jassim Al-Hatem
1,034 Points

I can't really understand this code

So I was scrolling down in treehouse's forum. And I found this code, that I understand but there's that one part that I'm struggling with:

def combo(iterable_1,iterable_2):
    list_of_tuples = []
    for index, item in enumerate(iterable_2, iterable_1[0]):
        list_of_tuples.append((index,item))
     return list_of_tuples

The 3rd line where it says enumerate(iterable_2, iterable_1[0]) can someone explain why he put in the [0] next to iterable_1.

[MOD: changed ''' to ``` formatting -cf]

2 Answers

🍹 Dave 🌴 StSomeWhere
🍹 Dave 🌴 StSomeWhere
17,003 Points

The second arg for enumerate is the starting int for the count (with a default of zero).

Your example is setting the start value to iterable_1[0] which must be an integer.

Does that help?

jassim Al-Hatem
jassim Al-Hatem
1,034 Points

Dave StSomeWhere,

I'm sorry for being inconvenience, But can you explain a little bit more.

🍹 Dave 🌴 StSomeWhere
🍹 Dave 🌴 StSomeWhere
17,003 Points

No inconvenience :smile:

In the code you posted - iterable_1 is being passed in and I'm not sure why someone would want to assume it is an integer and use only the first value (index zero) of that iterable. It's been a while since I've done that challenge and I'm pretty sure that the code you are referencing is incorrect and will not produce the desired result for the challenge. So, I feel that the code you are looking at is misleading. Regardless - understanding how enumerate() works what I think you are asking - right?

Does the below code and generated output answer your question:

iterable_variable = ["a", 1, "b"]

print('Lets enumerate starting at 0')
for counter, value in enumerate(iterable_variable):  # use default second arg with start = 0
    print('the value is -->', value)
    print('The counter is --> ', counter)

print('Lets enumerate starting at 57')
for counter, value in enumerate(iterable_variable, 57):  # specify a specific counter to start
    print('the value is -->', value)
    print('The counter is --> ', counter)

my_enumerate_object = enumerate(iterable_variable, 1)
print(type(my_enumerate_object))

my_object_as_list = list(my_enumerate_object)
print(my_object_as_list)

# output
Lets enumerate starting at 1
the value is --> a
The counter is -->  0
the value is --> 1
The counter is -->  1
the value is --> b
The counter is -->  2
Lets enumerate starting at 57
the value is --> a
The counter is -->  57
the value is --> 1
The counter is -->  58
the value is --> b
The counter is -->  59
<class 'enumerate'>
[(1, 'a'), (2, 1), (3, 'b')]
jassim Al-Hatem
jassim Al-Hatem
1,034 Points

Dave StSomeWhere,

Thank you so much, one last question:

print('Lets enumerate starting at 57')
for counter, value in enumerate(iterable_variable, 57):  # specify a specific counter to start
    print('the value is -->', value)
    print('The counter is --> ', counter)

does the placement matter?

for counter, value in enumerate(N1, N2):
    print('the value is -->', N1)
    print('The counter is --> ', N2)

is that right?

And legit bro, thank you so freaking much. I can go to sleep right now it's like 3 AM. And I'm going to skip today's lesson but I've learned something from you. cheers Dave!!!!!!!

🍹 Dave 🌴 StSomeWhere
🍹 Dave 🌴 StSomeWhere
17,003 Points

The placement does matter, the command is enumerate(iterable, start=0). The first parameter is required and must be the iterable. The second parameter is optional and must be an integer.

Here's the documentation:

Return an enumerate object. iterable must be a sequence, an iterator, or some other object which supports iteration. The next() method of the iterator returned by enumerate() returns a tuple containing a count (from start which defaults to 0) and the values obtained from iterating over iterable.

In your example N1 represents the individual entries of the iterable and N2 is the starting count value with a default of zero.

for counter, value in enumerate(N1, N2):
    print('the value is -->', N1) #  just an individual index entry from N1 

    #  The enumerate action
    print('The counter is --> ', N2) # equal to N2 only the first time, then N2 += 1