codescracker
python

Python One Dimensional Array (List) Program



This article contains multiple programs in Python, on one dimensional array, basically one dimensional list. Here is the simplest program on one dimensional array in Python:

arr = [1, 2, 3, 4, 5]
print(arr)

The output produced by above program on one dimensional list in Python is:

[1, 2, 3, 4, 5]

Now let's modify the above program, to print all elements of the array or list, one by one, instead of printing the whole list at once:

arr = [1, 2, 3, 4, 5]

for element in arr:
    print(element)

This time, the output will be:

1
2
3
4
5

Here is another version of the same program, that prints elements in same line, instead of multiple lines:

arr = [1, 2, 3, 4, 5]

for element in arr:
    print(element, end=" ")

Now the output will be:

1 2 3 4 5 

Here is the last program of this article, on one dimensional array or list in Python:

print("How many element to store in the list ? ", end="")
n = input()

arr = []
print("\nEnter", n, "Elements: ", end="")
n = int(n)
for i in range(n):
    element = input()
    arr.append(element)

print("\nThe list is:")
for i in range(n):
    print(arr[i], end=" ")

The snapshot given below shows the sample run of above program, with user input 6 as array or list size, and 32, 34, 35, 36, 40, 41 as six elements:

python one dimensional array program

Same Program in Other Languages

Python Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker