G
G
gylah-u2021-06-22 23:40:53
Python
gylah-u, 2021-06-22 23:40:53

How to find the number of columns in a python list?

I have two lists

first = [
['1', '2'],
['3', '4']
]

second = [
['1', '2', '3'],
['4', '5', '6']
]

How can I find out how many columns are in them, so that in the first I get 2, and in the second 3?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dmshar, 2021-06-23
@dmshar

The point is that what you showed is not a multidimensional array. It's just a list, each element of which contains the same list.
Why the question is not correct - well, try to answer the question, what should you get as a result if the list looks, for example, like this: You can of course adapt len(ifirst), len(ifirst(0)),len(ifirst(1) ) and iterate further - you can recursively. But if you really need arrays, then use numpy, and there is a .shape method directly intended to answer your question
ifirst=

D
Denis Melnikov, 2021-06-22
@Mi11er

Probably ...
print(max(list(map(len,second))))
The maximum of the number of elements will be displayed ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question