+ 3
I have a question in python lists
If we want to len a list for example list=[ 1,2,3,[4,5,6]] and then write print(len(list)) the output is 6 or 4 ?? And if we want to run this should we use def for list ? Im confused AF ~T_T~
14 odpowiedzi
+ 3
Hello my Friend, the answer is 4, and that's because remember, len gives you the total of Elements in the list, an element can be even a list, that's the reason
+ 5
Marvel, my question was if you will need to get the content from *list in list* in the way i isplayed it, or if you need to keep it as a list in list. Don't be confused!
+ 3
Do you need to get the content of the *list in list* like this: [1,2,3,4,5,6] ?
+ 1
Where did you get your SyntaxError?
+ 1
The output will be 4. Len function returns the number of elements in a list. Here there are for elements : 3 integers (1, 2 and 3) and a list.
#list
+ - 1 - + - 2 - + - 3 - + - ptr - +
|
------------------------------------
| #list in a list
+ - 4 - + - 5 - + - 6 - +
You see that you have 4 elements in variable 'list'.
+ 1
jose ramirez thanks
0
4, because it count [...] as one
0
Mirielle🐶 [Inactive] hmmm ... thanx
0
HonFu mmm im not sure it was syntax error but it didnt run .
0
Théophile oh ... thnq
0
Lothar no but now im curious#^_^# ... how can it be ?
0
Lothar both (:
0
It will print 4. To count all elements, you can use this code:
list = [1,2,3,4,[5,6,8],[7,8,6,4]]
length = len(list)
for i in list:
if(type(i) == type([])):
length += len(i) - 1
print(length)