Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 How to create a list with all values being zero 357mag Silly Frenchman Posts: 20 Threads: 13 Joined: Jul 2018 Reputation: 0 Likes received: 0 #1 Jul-13-2019, 08:42 PM (This post was last modified: Jul-13-2019, 08:44 PM by 357mag. Edited 2 times in total.) I've written a program that asks the user to input integers which will be placed in a list. I only want to input four integers. I'm looking for ways to make my list all zeros. Doesn't appear that this is as intuitive as arrays are in C++. Anyway this is what I got: ```numbers = [0] * 4 index = 0 while index < 4: numbers[index] = int(input("Enter a value for the list: ")) index += 1 print("\nHere is the values you entered: ", end='') for i in numbers: print(str(i) + " ", end='') print() print() ```According to my book the repetition operator makes multiple copies of a list and then it joins them all together. So is it doing numbers[0] + numbers[0] + numbers[0] + numbers[0] to finally get numbers[0, 0, 0, 0]? Isn't there an easier way to initialize all elements to zero? ichabod801 Bunny Rabbit Posts: 4,231 Threads: 94 Joined: Sep 2016 Reputation: 271 Likes received: 1262 #2 Jul-13-2019, 08:48 PM First, I'm not sure what's so hard about `numbers = [0] * 4`. That seems like an easy way to initialize a list to zeros. Second of all, you don't need to do that, since you are overwriting all the values anyway. In Python, lists do not have a set size, their size can change. The Pythonic way to get a list of four integers from the user is: ```numbers = [] for number in range(4): numbers.append(int(input('Enter a value for the list: '))) print('\nHere are the values you entered: {}.'.format(', '.join(str(number) for number in numbers))) ``` Craig "Ichabod" O'Brien - xenomind.com I wish you happiness. Recommended Tutorials: BBCode, functions, classes, text adventures 357mag Silly Frenchman Posts: 20 Threads: 13 Joined: Jul 2018 Reputation: 0 Likes received: 0 #3 Jul-14-2019, 07:20 AM When I tried the numbers [] the program threw an error message. So I presumed you cannot create a list that way. snippsat Posts: 4,021 Threads: 85 Joined: Sep 2016 Reputation: 301 Likes received: 1251 #4 Jul-14-2019, 08:20 AM `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.`````` ThomasL Minister of Silly Walks Posts: 351 Threads: 4 Joined: Jun 2019 Reputation: 62 Likes received: 83 #5 Jul-14-2019, 03:43 PM (Jul-14-2019, 07:20 AM)357mag Wrote: When I tried the numbers [] the program threw an error message. So I presumed you cannot create a list that way. Instead of whining around what about showing the error your program threw? `numbers []`will definitely throw an error because you need to write `numbers = []` « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Randomly assign values in List 1 to a value in List 2 Seneca260 7 572 Dec-16-2019, 12:57 PM Last Post: BamBi25 How do I map a list of values to specified colors? larkypython 4 234 Nov-05-2019, 09:22 AM Last Post: larkypython Create a dictionary from a list klllmmm 3 211 Oct-06-2019, 05:50 PM Last Post: Gribouillis Why does this list of lists end up with the same values alehak 2 369 Jul-18-2019, 12:34 PM Last Post: perfringo Remove \n from list of values within a pandas columns klllmmm 2 824 Jun-24-2019, 05:16 AM Last Post: klllmmm Create new list from another list based on condition khalidreemy 1 353 Jun-11-2019, 10:21 AM Last Post: buran update values in list based on dictionary bunti 3 881 Jun-10-2019, 07:26 AM Last Post: perfringo How to create meshgrid with non-integer stepsize of list elements? dazzabaijan 1 409 Mar-11-2019, 02:00 AM Last Post: ichabod801 Create a List for Boolean Mask. leoahum 4 732 Oct-09-2018, 02:38 PM Last Post: leoahum create list from repeated pattern in python Code4fun 2 1,019 Sep-25-2018, 07:09 PM Last Post: woooee

Forum Jump:

Users browsing this thread: 1 Guest(s)