Python Forum
How to create a list with all values being zero
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create a list with all values being zero
#4
numbers = [] is the preferred way in Python,do you get error when run @ichabod801 code?
# Using literals [],the prefered way
>>> numbers = []
>>> numbers
[]
>>> numbers.append(0)
>>> numbers
[0]
>>> numbers.append(99)
>>> numbers
[0, 99]
 
>>> # Function object list()
>>> numbers = list()
>>> numbers
[]

>>> # No need initialize list as may do in other langues as C/C++
>>> numbers = [0] * 4
>>> numbers
[0, 0, 0, 0]
If only shall display numbers in a string i would keep it as string,and only convert to int() if needed.
numbers = []
for number in range(4):
    numbers.append(input('Enter a value for the list: '))

print(f"\nHere are the values you entered: {','.join(numbers)}.")
Output:
λ python num.py Enter a value for the list: 10 Enter a value for the list: 0 Enter a value for the list: 0 Enter a value for the list: 999 Here are the values you entered: 10,0,0,999.
Reply


Messages In This Thread
RE: How to create a list with all values being zero - by snippsat - Jul-14-2019, 08:20 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Copying the order of another list with identical values gohanhango 7 1,199 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Search Excel File with a list of values huzzug 4 1,297 Nov-03-2023, 05:35 PM
Last Post: huzzug
  Comparing List values to get indexes Edward_ 7 1,231 Jun-09-2023, 04:57 PM
Last Post: deanhystad
  Delete strings from a list to create a new only number list Dvdscot 8 1,593 May-01-2023, 09:06 PM
Last Post: deanhystad
  Adding values with reduce() function from the list of tuples kinimod 10 2,747 Jan-24-2023, 08:22 AM
Last Post: perfringo
  user input values into list of lists tauros73 3 1,101 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  [split] why can't i create a list of numbers (ints) with random.randrange() astral_travel 7 1,571 Oct-23-2022, 11:13 PM
Last Post: Pedroski55
  Create array of values from 2 variables paulo79 1 1,118 Apr-19-2022, 08:28 PM
Last Post: deanhystad
  AttributeError: 'list' object has no attribute 'values' ilknurg 4 15,126 Jan-19-2022, 08:33 AM
Last Post: menator01
  Need to parse a list of boolean columns inside a list and return true values Python84 4 2,144 Jan-09-2022, 02:39 AM
Last Post: Python84

Forum Jump:

User Panel Messages

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