Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
list - 2 questions:
#11
(Jul-31-2021, 12:05 PM)ndc85430 Wrote: The code is still wrong, isn't it? You need to be checking the length of my_list, not modified_list (and the same for the slice) Think about what you're doing - modified_list is the one you're using to hold the return value, my_list is the original, so of course you want to do the operations on the original.

yea, that is what I did, i checked the len of the my_list, and not modified_list. but you told me in the other comment that i need to replace all my_list in modified_list, so i re-uploaded it as modified_list, but then you told me its not what you meant by that.
I cant really understand what you say to me Sad ill try to read again, but I dont think..
Im trying something like that:
i did my_list as the name of the func and added [] ( empty list ).
then checked lenght of it.
uh im confused myself now... i guess I wont be able to complete it...
Reply
#12
(Jul-31-2021, 12:08 PM)ben1122 Wrote: but you told me in the other comment that i need to replace all my_list in modified_list

Emphasis mine. No I did not. I said you needed to use a different variable name for the modified list, that's all. If it makes life simpler, just get rid of modified_list if you don't want to assign to it.
Reply
#13
(Jul-31-2021, 12:10 PM)ndc85430 Wrote:
(Jul-31-2021, 12:08 PM)ben1122 Wrote: but you told me in the other comment that i need to replace all my_list in modified_list

Emphasis mine. No I did not. I said you needed to use a different variable name for the modified list, that's all. If it makes life simpler, just get rid of modified_list if you don't want to assign to it.

okay, so I returned to this state:
def format_list(my_list):
    """
    This function returns the words in the list which is in the even side.
    :param my_list: The list of the project.
    :type my_list: str, int, float
    :rtype: str, int, float
    :return: The result of the list on the even only.
    """
    my_list = []
    if len(my_list) % 2 == 0:
        return my_list[0::2]


new_list = format_list(my_list=["hydrogen", "helium", "lithium", "beryllium", "boron", "magnesium"])
print(new_list)
its the beginning one.
I still dont know what is my mistake in here...

I removed the thing you told me at start, so its like that now:
def format_list(my_list):
    """
    This function returns the words in the list which is in the even side.
    :param my_list: The list of the project.
    :type my_list: str, int, float
    :rtype: str, int, float
    :return: The result of the list on the even only.
    """
    my_list = []
    if len(my_list) % 2 == 0:
        return my_list[0::2]


print(format_list(my_list=["hydrogen", "helium", "lithium", "beryllium", "boron", "magnesium"]))
but still output is the same [], nothing I do changes it.
Reply
#14
def format_list(my_list):
    """
    This function returns the words in the list which is in the even side.
    :param my_list: The list of the project.
    :type my_list: str, int, float
    :rtype: str, int, float
    :return: The result of the list on the even only.
    """
    if len(my_list) % 2 == 0:
        return my_list[0::2]


print(format_list(my_list=["hydrogen", "helium", "lithium", "beryllium", "boron", "magnesium"]))
oh wait , I changed something and now its worked.
i removed the my_list = []
why did it suddnely work?
Reply
#15
['hydrogen', 'lithium', 'boron']
its the output, its good i think no?
Reply
#16
I cant seem to understand, if I removed my_list = [] and its good now, why is it like that?
it means that my_list shouldnt be a list, but it is a list now, athough i removed [].

any explanation please for it? as I know, in order for a variable to be a list, it has to have [] or something else. but in here it doesnt have it.
Reply
#17
Why did you think you needed the line my_list = [] in the first place? All it was doing was creating a local variable called my_list that was shadowing the function parameter, so that the function parameter was inaccessible.
ben1122 likes this post
Reply
#18
(Jul-31-2021, 12:23 PM)ndc85430 Wrote: Why did you think you needed the line my_list = [] in the first place? All it was doing was creating a local variable called my_list that was shadowing the function parameter, so that the function parameter was inaccessible.

because otherwise it wouldnt count as a list, but I just realized that the new_list thingy, because it has there My_list and i did there [] with stuff in it, it counts as list.
that thing is new to me, thanks :)
about the last thing of the question, adding the and magnesium.
if im doing new_list[-1] ill get: and boron ( of course, because new list is a other list with jumps of 2 ).
if ill try my_list, it wont work, because its not a variable.
what am i supposed to do now?
I tried to create a new variable of list and then modify it to have the last -1 of the my_list, but nothing seems to work.
Reply
#19
I don't understand what you're asking. A better way to go about this is to show what input you're giving, what you expect to get out and the code you're trying to achieve that.
Reply
#20
I tried to do something global and then use it, but for some reason global is not working here...

def format_list(my_list):
    """
    This function returns the words in the list which is in the even side.
    :param my_list: The list of the project.
    :type my_list: str, int, float
    :rtype: str, int, float
    :return: The result of the list on the even only.
    """

    if len(my_list) % 2 == 0:
        joined_list = my_list[-1]
        global joined_list
        return my_list[0::2]

new_list = format_list(my_list=["hydrogen", "helium", "lithium", "beryllium", "boron", "magnesium"])
print(new_list)
joined_list # Here its not working, the global, it does nothing, why?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Discord bot that asks questions and based on response answers or asks more questions absinthium 1 38,825 Nov-25-2017, 06:21 AM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020