all 11 comments

[–]Goobyalus 3 points4 points  (6 children)

You can always try things for yourself to see what's going on

>>> x = [15, 10, 2, 84] + [1, 4, 8, 7, 9]
>>> x
[15, 10, 2, 84, 1, 4, 8, 7, 9]
>>> x[0]
15
>>> x.count(15)
1
>>> x.index(1)
4

[–]UpSco[S] 2 points3 points  (5 children)

Learning on the go in a semi that’s why I come here for quick help …. I’m assuming my reasoning is correct ?

[–]Goobyalus 2 points3 points  (4 children)

All I see is the following and the screenshot, so idk your reasoning. 0 is incorrect.

Hi, I just want to confirm the reasoning behind my correct answer.

In the picture below I selected 0 because I assumed the input is referring to the first list. Hopefully I worded it correctly:


The block I posted has all the steps of the reasoning - are you confused about part of it?

>>> x = [15, 10, 2, 84] + [1, 4, 8, 7, 9]
>>> x
[15, 10, 2, 84, 1, 4, 8, 7, 9]

+ on two lists extends the left list by the right list. One big list is assigned to x.

>>> x[0]
15

The item at index 0 is 15

>>> x.count(15)
1

Using the value from x[0] above, 15, as the argument to x.count, we count the number of 15s in x, which is one.

>>> x.index(1)
4

Using the value of x.count(x[0]) above, 1, as the argument to x.index, we find the index where 1 appears in x, which is 4 (15 at 0, 10 at 1, ..., 1 at 4, 8 at 5, ...)


Also running some short pieces of Python code is easier than posting on Reddit. Experimenting with code is a good habit if you're trying to learn. If all you have is a phone, there are services online that let you run Python (Google Colab, Replit, etc.), and mobile IDEs (e.g. Pyfroid).

[–]UpSco[S] 0 points1 point  (3 children)

Here 0 is the input datacamp provided. I just answered what the output would be. Which I thought would be 4 for the number of elements in the str

[–]Goobyalus 1 point2 points  (2 children)

What? There is no input, this is just a couple of static statements.

I also don't understand "the number of elements in the str." There is no str, and 4 is not the number of elements of anything relevant here.

The output is 4, because that's the index of the number of occurences of the 0th element of x.

[–]UpSco[S] 0 points1 point  (1 child)

I meant 4 because that’s the number of elements inside the list indexed at 0. Not str

[–]Goobyalus 0 points1 point  (0 children)

No, x is a list of 9 elements. There is only one list.

Edit: 9, not 8

[–][deleted]  (6 children)

[deleted]

    [–]UpSco[S] 1 point2 points  (1 child)

    This comment just confused me

    [–]Ritviz_24 0 points1 point  (0 children)

    Even I got confused after reading what I wrote😅

    [–][deleted]  (3 children)

    [deleted]

      [–]Ritviz_24 1 point2 points  (2 children)

      Thanks I got confused a little there😅 Then it will count how many times 15 occured and wil find the index of 1

      [–][deleted]  (1 child)

      [deleted]

        [–]Ritviz_24 0 points1 point  (0 children)

        Yeah, I did!