Jump to content

A question regarding index and lists on Python.

LebowskiBuschemi

Hello, so as some of you may know from a previous post of mine, I recently started learning Python and now, I've moved on to lists in Python and I'm currently having trouble understanding the code attached to this post. What I understand from the code is that we've defined a function that basically tells you whether a person is late or not using the name argument and the way the person was able to use the name argument to traverse the list was through the use of the .index command which is basically being used to check whether any of the user inputs for the name argument is indexed within the list. Finally, the return command is just returning location of the indexed name within list that meets the two conditions. 

PythonList.JPG

Link to comment
Share on other sites

Link to post
Share on other sites

list.index() returns the index of the first found value matching the parameter.

eg. list = [1,2,3,3]

 

list.index(3) will return 2.

 

If the value isn't in the list, it will throw an exception.

 

Your return gives you a boolean value if it meets those conditions based on that returned value from index()

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Slottr said:

list.index() returns the index of the first found value matching the parameter.

eg. list = [1,2,3,3]

 

list.index(3) will return 2.

 

If the value isn't in the list, it will throw an exception.

 

Your return gives you a boolean value if it meets those conditions based on that returned index

Is it safe to say .index connects the name argument to the list itself in order to search the list for the indexed value that was given through user input?

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, LebowskiBuschemi said:

Is it safe to say .index connects the name argument to the list itself? 

 

Name is just a parameter, so when you give it a string eg. list.index("John") it will iterate the list to fine a match. The type can be anything

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, Slottr said:

 

Name is just a parameter, so when you give it a string eg. list.index("John") it will iterate the list to fine a match. The type can be anything

Alright, thanks!

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, Slottr said:

If the value isn't in the list, it will throw an exception.

It's probably not a great idea to outright call .index like that in the real world, unless there is something stated in the docstring of this behavior. Also, it would be helpful to know an example of the expected list and name.

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, noitcelfeR said:

It's probably not a great idea to outright call .index like that in the real world, unless there is something stated in the docstring of this behavior. Also, it would be helpful to know an example of the expected list and name.

It is, and also the reason we have try-catch blocks.

 

I'm not the OP though. You may want to raise this question to them rather than myself.

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, Slottr said:

It is, and also the reason we have try-catch blocks.

 

I'm not the OP though. You may want to raise this question to them rather than myself.

Using try / catch blocks for business logic is generally bad practice. They are reserved for exceptional situations (hence, the name). However, there are reasons for ignoring something like this. Design by Contract would generally allow this if preconditions are listed in the docs. 

Link to comment
Share on other sites

Link to post
Share on other sites

25 minutes ago, noitcelfeR said:

Using try / catch blocks for business logic is generally bad practice. They are reserved for exceptional situations (hence, the name). However, there are reasons for ignoring something like this. Design by Contract would generally allow this if preconditions are listed in the docs. 

Totally unrelated to the topic at hand.

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

11 hours ago, noitcelfeR said:

It's probably not a great idea to outright call .index like that in the real world, unless there is something stated in the docstring of this behavior. Also, it would be helpful to know an example of the expected list and name.

This is just a code snippet, there may be a check before this function is called, i.e.

if name in arrivals:
	late = fashionably_late(arrivals, name)

 

Don't ask to ask, just ask... please 🤨

sudo chmod -R 000 /*

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×