-4

How can I print elements of an array without the double brackets [[]] ?

Let's say I have an array:

A = [[1]
     [2]
     [3]
     [4]]

and I want to print the elements separately like this:

Output:
1
2
3
4

Here's my attempt:

for i in range(4):
    print(A[i])
Output:

[[1]]
[[2]]
[[3]]
[[4]]

1 Answers1

1

You need to know the indexing, as i can see, you have list of lists, and you need first value of each list in a list. So you can loop through it and output the first value:

for i in A:
    print(i[0])

Or if you have single values inside lists, you could omit them:

A = [1, 2, 3, 4, 5]
for i in A:
    print(A)
Michael
  • 657
  • 4
  • 29