+ 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~

28th Oct 2019, 11:05 AM
Marvel
Marvel - avatar
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
29th Oct 2019, 12:55 PM
Esteban Alexis Arce Gómez
Esteban Alexis Arce Gómez - avatar
+ 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!
28th Oct 2019, 3:48 PM
Lothar
Lothar - avatar
+ 3
Do you need to get the content of the *list in list* like this: [1,2,3,4,5,6] ?
28th Oct 2019, 11:41 AM
Lothar
Lothar - avatar
+ 1
Where did you get your SyntaxError?
28th Oct 2019, 11:29 AM
HonFu
HonFu - avatar
+ 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'.
28th Oct 2019, 11:33 AM
Théophile
Théophile - avatar
+ 1
28th Oct 2019, 12:52 PM
Marvel
Marvel - avatar
0
4, because it count [...] as one
28th Oct 2019, 12:25 PM
jose ramirez
jose ramirez - avatar
0
28th Oct 2019, 12:48 PM
Marvel
Marvel - avatar
0
HonFu mmm im not sure it was syntax error but it didnt run .
28th Oct 2019, 12:50 PM
Marvel
Marvel - avatar
0
Théophile oh ... thnq
28th Oct 2019, 12:51 PM
Marvel
Marvel - avatar
0
Lothar no but now im curious#^_^# ... how can it be ?
28th Oct 2019, 12:52 PM
Marvel
Marvel - avatar
0
Lothar both (:
29th Oct 2019, 8:20 PM
Marvel
Marvel - avatar
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)
30th Oct 2019, 7:46 AM
Syed Mishar Newaz
Syed Mishar Newaz - avatar