Python Forum

Full Version: how to remove brackets within a list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hello,

I would like to know how to get rid of brackets within a list. Ex. ['1', 'r', (2, '5')]. The round brackets inside this list, I would like to get rid of. 

Here is the code in question:

#function for requesting numerical mark --> ask if this function is necessary because computeGrade prompts for both the mark and gives the letter grade.
def getMark():
    mark = int(input("What is your mark? (eg. 50) "))
    return mark


#function for letter grades
def computeGrade():
    #must get the grade from function getMark() or it will keep asking you for inputs each step of the way, when entering getMark in as a function
    gr = getMark()
    if gr < 50:
        g = "F"
        print(g)
    elif gr > 49 and gr < 55:
        g = "D"
        print(g)
    elif gr > 54 and gr < 65:
        g = "C"
        print(g)
    elif gr > 64 and gr < 80:
        g = "B"
        print(g)
    elif gr >= 80:
        g = "A"
        print(g)
    else: #how to make it so if they enter anything other than numerical values it gives you an error message?
        print("Invalid input, please enter your numerical grade such as 40, 80, or 90")
    return(gr, g)
        
#making the list using previous functions        
def student():
    #studlist = [getLastname(), getFirstname(), getMark(), computeGrade()]
    studlist = [getLastname(), getFirstname(), computeGrade()]
    return studlist

print(student())
I would like to eventually get this to print off an index with no brackets what so ever. But I have seen a tutorial on how to do that in the "list basic" thread. Unfortunately, the procedure will print with the round brackets included for the above code.

Look forward to your responses.

Thank you,
A3G
As your code returns the values like that you cannot, instead of writing more unnecessary code. computeGrade() returns a tuple which is represented with brackets.

def computeGrade():
    .....
    .....
    return [getLastname(), getFirstname(), getMark(), gr, g]
Or return a list instead of tuple.

def computeGrade():
    .....
    .....
    return [gr, g]

studlist = [getLastname(), getFirstname(), computeGrade()].extend(computeGrade())
The bracket means there is a tuple in that position of the list.
From your code it seems that the function computeGrade() returns this tuple, but this function is not shown in your code, neither is getLastname() or getFirstname().

You can use this function from the python documentation in itertools reciepe to flatten the list.
from itertools import chain

my_list = ['1', 'r', (2, '5')]

def flatten(listOfLists):
   "Flatten one level of nesting"
   return chain.from_iterable(listOfLists)

my_flattened_list = list(flatten(my_list))
print(my_flattened_list)
Output:
['1', 'r', 2, '5']
(Oct-16-2016, 11:05 AM)Yoriz Wrote: [ -> ]From your code it seems that the function computeGrade() returns this tuple, but this function is not shown in your code, neither is getLastname() or getFirstname().
Actually it is ;)

I didn't know about itertools.chain. Thanks!
So it is.

You could also change the function student to
#making the list using previous functions        
def student():
    gr, g = computeGrade()
    studlist = [getLastname(), getFirstname(), gr, g]
    return studlist
Thank you for the prompt reply.

But when I enter this in:

from itertools import chain
my_list = [student()]
def flatten(listOfLists):
    return chain.from_iterable(listOfLists)
my_flattened_list = list(flatten(my_list))
print(my_flattened_list)
It returns -->['lastname', 'firstname', (mark, 'letter grade)]

sorry did not see your second post Yoriz.

That piece of code is much easier to understand and it works... but it prompts me for my mark before asking for my last name and first name.... how do i make it so the order asks for last name and first name then mark and gives grade?

def student():
    #studlist = [getLastname(), getFirstname(), getMark(), computeGrade()]
    gr, g = computeGrade()
    studlist = [getLastname(), getFirstname(), gr, g]
    return studlist
returns the proper list that i was after! but need the correct order of question.
Also you can change getting the Mark a bit

while True:
     try:
         mark = int(input("What is your mark? (eg. 50) "))
         break
     except ValueError:
         print("Invalid input. Try again!)
(Oct-16-2016, 11:25 AM)wavic Wrote: [ -> ]Also you can change getting the Mark a bit

while True:
     try:
         mark = int(input("What is your mark? (eg. 50) "))
         break
     except ValueError:
         print("Invalid input. Try again!)

awesome thank you! I guess that is a great way of fixing why the else statement did not work in compute grade section =)
this will fix the order
def student():
    last_name = getLastname()
    first_name = getFirstname()
    gr, g = computeGrade()
    studlist = [last_name, first_name, gr, g]
    return studlist
Note: Flaten will not be any good if the strings in the list contain more than 1 character as they will also be split into single items in the list.
(Oct-16-2016, 11:31 AM)Yoriz Wrote: [ -> ]this will fix the order
def student():
    last_name = getLastname()
    first_name = getFirstname()
    gr, g = computeGrade()
    studlist = [last_name, first_name, gr, g]
    return studlist
Note: Flaten will not be any good if the strings in the list contain more than 1 character as they will also be split into single items in the list.

It worked!!! Much appreciated =)
Pages: 1 2