Python Forum
how to remove brackets within a list
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to remove brackets within a list
#11
As I'm not spotting the  obvious things today.
there is no need to assign the list to a variable and then return it , you can return it directly
def student():
    last_name = getLastname()
    first_name = getFirstname()
    gr, g = computeGrade()
    return [last_name, first_name, gr, g]
Reply
#12
(Oct-16-2016, 11:31 AM)A3G Wrote: awesome thank you! I guess that is a great way of fixing why the else statement did not work in compute grade section =)
else doesn't matter. Once gerMark() is called and if the input is incorrect it will throw an error and program is going to exit with an error message. It's always better to handle the inputs in place. Else is used if the input is out of range. But I am talking about if one enter something like 'GGBG' instead of numerical input
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#13
thank you to you both!

I am obviously new to this, great to have some excellent and fast feed back from the two of you!

P.S. this is for my python class, but did not put in to the home work section because I thought the question was specific enough to be here.

If you two are able to help me with my next section of making a function that adds the inputs from the list we just created, that would be much appreciated (have been working on this for a couple of days now, cant seem to get the list to add on properly)......

#creating a loop to ask for additional student inputs
def classlist():
    itemList = []
    stud = student()
    itemList.append(stud)
    anyM = input("Any more students in your class? (yes/no) ")
    if anyM == "yes":
        stud = student()
        itemList.append(stud)
    if anyM == "no":
        return itemList    
    while stud: #something about this while loop is not working when i input no
        anyM = input("Any more students in your class? (yes/no) ")
        itemList.append(stud)
        if anyM == "yes":
            anyM = input("Any more students in your class? (yes/no) ")
            itemList.append(stud)
        if anyM == "no":
            return itemList
        
print(classlist())
Reply
#14
There is a lot of duplication in your code.
It can be simplified to
#creating a loop to ask for additional student inputs
def classlist():
   itemList = [student()]
   while True:
       anyM = input("Any more students in your class? (yes/no) ")
       if anyM == "yes":
           itemList.append(student())
       elif anyM == "no":
           return itemList    
        
print(classlist())
Reply
#15
Yoriz, you are a savior.... thank you so much...

if you have time would you please explain what the difference is between an IF and ELIF?

I have been using if and if and if and if, not really using ELIF.... is there a special reason that we use ELIF instead of IF?
Reply
#16
When just ifs are used everyone of them will have their condition checked, when you use elif the condition will only be checked if a previous if or elif condition was not met.
So in the case of the code I posted last, if the input is yes it wont bother checking if the input was no.
Reply
#17
thank you!
Reply
#18
Elif is short for else if. As knowing that you can guess it.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  unable to remove all elements from list based on a condition sg_python 3 377 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  Remove numbers from a list menator01 4 1,251 Nov-13-2022, 01:27 AM
Last Post: menator01
  [SOLVED] [BeautifulSoup] Why does it turn inserted string's brackets into </>? Winfried 0 1,452 Sep-03-2022, 11:21 PM
Last Post: Winfried
  Reading list items without brackets and quotes jesse68 6 4,523 Jan-14-2022, 07:07 PM
Last Post: jesse68
  Remove empty keys in a python list python_student 7 2,901 Jan-12-2022, 10:23 PM
Last Post: python_student
  Data pulled from SQL comes in brackets nickzsche 3 2,549 Jan-04-2022, 03:39 PM
Last Post: ibreeden
  For Loop and Use of Brackets to Modify Dictionary in Tic-Tac-Toe Game new_coder_231013 7 2,169 Dec-28-2021, 11:32 AM
Last Post: new_coder_231013
  Remove an item from a list contained in another item in python CompleteNewb 19 5,552 Nov-11-2021, 06:43 AM
Last Post: Gribouillis
  Getting a certain value from inside brackets. LeoT 5 2,940 Mar-01-2021, 03:34 PM
Last Post: buran
  .remove() from a list - request for explanation InputOutput007 3 2,177 Jan-28-2021, 04:21 PM
Last Post: InputOutput007

Forum Jump:

User Panel Messages

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