user18585713
user18585713

Reputation:

How to iterate and print each element in list (Python)

arr(['36 36 30','47 96 90','86 86 86']

I want to store and print the values like this,

36
36 
30
47
...

How do I do this using python?

Upvotes: 2

Views: 39

Answers (3)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521093

We can try the following approach. Build a single string of space separated numbers, split it, then join on newline.

inp = ['36 36 30', '47 96 90', '86 86 86']
output = '\n'.join(' '.join(inp).split())
print(output)

This prints:

36
36
30
47
96
90
86
86
86

Upvotes: 1

SAI SANTOSH CHIRAG
SAI SANTOSH CHIRAG

Reputation: 2084

You can use lists and split in python. Try in this way:

arr = ['36 36 30','47 96 90','86 86 86']
for i in arr:
    elems = i.split()
    for elem in elems:
        print(elem)

Upvotes: 1

toppk
toppk

Reputation: 700

the simplest way is to use for and str.split()

arr=['36 36 30','47 96 90','86 86 86']
for block in arr:
    cells = block.split()
    for cell in cells: 
        print(cell)

prints

36
36
30
47
96
90
86
86
86

you can also use a list comprehension like so, which returns the same result.

print("\n".join([ele for block in arr for ele in block.split()]))

Upvotes: 2

Related Questions