Python Forum
Displaying list correspond to the column number
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Displaying list correspond to the column number
#1
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.
Reply
#2
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
Reply
#3
(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
danlopek14q likes this post
Reply
#4
Perfect that will work. But how to have the effect like this ['Mars','Hot'] not
Mars
Hot
Reply
#5
(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
Reply
#6
(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.
danlopek14q likes this post
Reply
#7
I tried that but then are two lists ['Hot']['Mars'] how can I achieve the effect like ['Hot','Mars'] ?
Reply
#8
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 :)
Reply
#9
(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))
Reply
#10
(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']
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  list digit into number Voldyy 2 1,523 Jul-10-2022, 06:13 PM
Last Post: deanhystad
  How to convert every even number in a list to odd? Bruizeh 4 3,736 Aug-27-2021, 03:04 AM
Last Post: naughtyCat
  Get the biggest number from a two dimensional list rs74 13 4,031 Aug-09-2020, 04:02 PM
Last Post: deanhystad
  How can I print the number of unique elements in a list? AnOddGirl 5 3,251 Mar-24-2020, 05:47 AM
Last Post: AnOddGirl
  Multiplying number in a list in an order pythoneer 12 6,564 Mar-23-2018, 08:21 PM
Last Post: buran
  Displaying number of currency danellapotter 3 2,753 Jan-16-2018, 07:58 PM
Last Post: buran
  adding a number to the list atux_null 4 3,845 Nov-06-2017, 07:01 PM
Last Post: gruntfutuk
  Displaying a long list with Rows and Columns ngr33n 5 10,968 Sep-21-2017, 10:17 PM
Last Post: ngr33n
  sorted list not displaying Crackity 6 5,050 Jul-18-2017, 12:50 PM
Last Post: sparkz_alot
  Determine if a list contains a specific number of an item flannel_man 3 4,882 Nov-12-2016, 04:46 PM
Last Post: micseydel

Forum Jump:

User Panel Messages

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