Python Forum
Displaying list correspond to the column number - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: Displaying list correspond to the column number (/thread-33174.html)



Displaying list correspond to the column number - danlopek14q - Apr-04-2021

Hello guys

I have a problem finding a solution for the lists.
I have a function all_list which takes two parameters (listas, column[]). Inside of the column parameter User chooses to pass for example two values [0,1] , those numbers correspond to the element inside of the list which should be display. For example : all_list (['Mars','Hot',22,True],column[0,1]. The display values should be ['Mars','Hot'] if no column value then all list should be display like ['Mars','Hot',22,True]

def all_list(listas,column[]):
    #for no columns value
    if column == []:
        print(listas)
    #how to do when user use [0,2]or [1,2] etc.



RE: Displaying list correspond to the column number - danlopek14q - Apr-04-2021

def all_list(listas,column[]):
    #for no columns value
    if column == []:
        print(listas)
    elif column == [0, 1]:
         print([listas[0], listas[1]])
    elif column == [0, 2]:
         print([listas[0], listas[2]])
    elif column == [0, 3]:
         print([listas[0], listas[3]])
    elif column == [1, 0]:
         print([listas[1], listas[0]])
[/quote]

But this is not how it should look but it works


RE: Displaying list correspond to the column number - ibreeden - Apr-04-2021

(Apr-04-2021, 10:02 AM)danlopek14q Wrote: def all_list(listas,column[]):
This is not correct. If you want to give a default for a parameter you should write:
def all_list(listas,column = []):
If a column list is given, you can iterate over that list:
    for i in column:
        print(listas[i])
That will do the trick.

the_list = ["a", "b", "c"]
all_list(the_list)
all_list(the_list, [0, 2])
Output:
['a', 'b', 'c'] a c



RE: Displaying list correspond to the column number - danlopek14q - Apr-04-2021

Perfect that will work. But how to have the effect like this ['Mars','Hot'] not
Mars
Hot


RE: Displaying list correspond to the column number - danlopek14q - Apr-04-2021

(Apr-04-2021, 10:23 AM)danlopek14q Wrote:
def all_list(listas,column[]):
    #for no columns value
    if column == []:
        print(listas)
    elif column == [0, 1]:
         print([listas[0], listas[1]])
    elif column == [0, 2]:
         print([listas[0], listas[2]])
    elif column == [0, 3]:
         print([listas[0], listas[3]])
    elif column == [1, 0]:
         print([listas[1], listas[0]])

But this is not how it should look but it works
[/quote]

Yes the column will be given by the user but instead of printing all list like ['Mars',99] is print one under another


RE: Displaying list correspond to the column number - ibreeden - Apr-04-2021

(Apr-04-2021, 10:40 AM)danlopek14q Wrote: Perfect that will work. But how to have the effect like this ['Mars','Hot'] not
Mars
Hot

If you want to have the result printed as a list, you should make a result list and print the result at the end.
        returnlist = []
        for i in column:
            returnlist.append(listas[i])
        print(returnlist)
Test:
the_list = ["a", "b", "c"]
all_list(the_list)
all_list(the_list, [2, 0, 1, 1, 1])
Output:
['a', 'b', 'c'] ['c', 'a', 'b', 'b', 'b']
As an alternative you could instead use the "end" parameter of the print function. Like:
print(listas[i], end=", "
This would cause the next print not to start on a new line.


RE: Displaying list correspond to the column number - danlopek14q - Apr-04-2021

I tried that but then are two lists ['Hot']['Mars'] how can I achieve the effect like ['Hot','Mars'] ?


RE: Displaying list correspond to the column number - danlopek14q - Apr-04-2021

Everything works, Thank You so much for your time and help. I may have more questions in the future, but it definitely saved my day. Happy Easter :)


RE: Displaying list correspond to the column number - naughtyCat - Aug-27-2021

(Apr-04-2021, 10:37 AM)ibreeden Wrote:
(Apr-04-2021, 10:02 AM)danlopek14q Wrote:
this is not correct
def all_list(listas,column = []): # this is wrong
for example:
def func(arr = []):
    print(arr)
    arr.append(2)

func() # each time this function is called, the default arg will change
func()
func()
func()
Output:
[] [2] [2, 2] [2, 2, 2]
The correct code with default list should be:
Choice 1:
def func(arr = None):
    if arr == None:
        arr = []
    return arr

print(func([1, 2, 3, 4]))
Choice 2:
def func(*args):
    arr = list(args)
    return arr

print(func(1, 2, 3, 4))



RE: Displaying list correspond to the column number - naughtyCat - Aug-27-2021

(Apr-04-2021, 10:02 AM)danlopek14q Wrote:
def func(arr, col = None):
    if col == None: return ''
    return [arr[i] for i in col]

arr = ['Mars', 'Hot', 22, True]
column = [0, 1]
print(func(arr, column))
Output:
['Mars', 'Hot']