Python Forum
Question about list and while function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question about list and while function
#4
(Oct-11-2019, 03:54 AM)doug2019 Wrote: the problem is that list must always have 10 elements and "a" and "b" will be input by the user

Will this meet your needs?

a = 1
b = 5
mlist= [0]*(a - 1) + [a * (b - x) for x in range(a, b+1)] + [0]*(10 - b)
print(mlist)
Output:
[4, 3, 2, 1, 0, 0, 0, 0, 0, 0]

If you need to have the calculated values in ascending order, you could try the following:

a = 1
b = 5
mlist= [0]*(a - 1) + [a * (b - x) for x in range(b, a-1, -1)] + [0]*(10 - b)
print(mlist)
Output:
[0, 1, 2, 3, 4, 0, 0, 0, 0, 0]
A.D.Tejpal
Reply


Messages In This Thread
RE: Question about list and while function - by adt - Oct-11-2019, 08:45 AM
RE: Question about list and while function - by adt - Oct-11-2019, 12:02 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Question on dir() function Soorya25 1 1,250 Jan-16-2023, 09:33 PM
Last Post: deanhystad
  Function not scriptable: Noob question kaega2 3 1,269 Aug-21-2022, 04:37 PM
Last Post: kaega2
  input function question barryjo 12 2,947 Jan-18-2022, 12:11 AM
Last Post: barryjo
  Question on None function in a machine learning algorithm Livingstone1337 1 2,413 Mar-17-2021, 10:12 PM
Last Post: supuflounder
  Question in python function problem saratha 1 1,510 Jul-08-2020, 04:56 PM
Last Post: jefsummers
  question about python3 print function jamie_01 5 2,752 May-25-2020, 09:58 AM
Last Post: pyzyx3qwerty
  Question about the groupby function new_to_python 7 3,375 Feb-09-2020, 07:52 AM
Last Post: perfringo
  I created a function that generate a list but the list is empty in a new .py file mrhopeedu 2 2,387 Oct-12-2019, 08:02 PM
Last Post: mrhopeedu
  Question on Join() function sduvvuri 2 2,841 Jun-02-2019, 03:55 PM
Last Post: perfringo
  about List question longmail 2 2,460 Nov-30-2018, 02:08 AM
Last Post: longmail

Forum Jump:

User Panel Messages

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