Why we use Len() | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
22nd Jan 2021, 4:16 PM
Mohamed Ahmed Abdel Atty
Mohamed Ahmed Abdel Atty - avatar
16 Answers
+ 4
len short form for length. Example: a = ["a", "b", "c"] len(a) this will give the number of elements available in the list a, in this case the length of a is 3.
22nd Jan 2021, 4:19 PM
Rohit Kh
Rohit Kh - avatar
+ 4
Mohamed Ahmed Abdel Atty It looks like you have forgotten a lot of python, I'll recommend to re-learn it with proper understanding and start making code using it.
22nd Jan 2021, 5:14 PM
Rohit Kh
Rohit Kh - avatar
+ 3
lst[i] so when i=0, lst[0] is equal to zero index in lst which is 'a' in the lst we have.
22nd Jan 2021, 5:12 PM
Rohit Kh
Rohit Kh - avatar
+ 1
lst = ['a', 'b', 'c', 'g', 'i', 'a', 'a'] for i in range(len(lst)): if lst[i] == 'a': lst[i] = None print(lst)
22nd Jan 2021, 4:25 PM
Mohamed Ahmed Abdel Atty
Mohamed Ahmed Abdel Atty - avatar
+ 1
Why we used len() here
22nd Jan 2021, 4:26 PM
Mohamed Ahmed Abdel Atty
Mohamed Ahmed Abdel Atty - avatar
+ 1
Mohamed Ahmed Abdel Atty We use for i in range(len(lst)): because we want the index of the lst and not the element for the code logic to work because we can see that we are checking finding all the "a" in the list lst. so we're checking is lst[0] == 'a'? lst[1] == 'a'? ... lst[6] == 'a'? So you can see that we i of for loop to be number that's we used it that way. check this code to understand the difference - lst = ['a', 'b', 'c', 'g', 'i', 'a', 'a'] print(len(lst),'\n') # its 7 for i in lst: print(i) print() for i in range(len(lst)): print(i) ''' this is equivalent to because len(lst) equals 7 here for i in range(7): print(i) '''
22nd Jan 2021, 4:37 PM
Rohit Kh
Rohit Kh - avatar
+ 1
Mohamed Ahmed Abdel Atty vertical is because of this \n after printing each output move to next line with the help of \n
22nd Jan 2021, 4:53 PM
Rohit Kh
Rohit Kh - avatar
+ 1
to check if any element in lst is equal to 'a' or not.
22nd Jan 2021, 5:10 PM
Rohit Kh
Rohit Kh - avatar
0
It's a function that helps to know the length of the list by knowing the number of items inside it
23rd Jan 2021, 6:44 AM
mo'men said
mo'men said - avatar
0
Len()function is use to calculate length of the given object. For eg consider below code mylist=[(1,2),(3,4),(5,6)] len(mylist) Output=3 we get length as 4 because above list contains 3 elements
23rd Jan 2021, 10:59 AM
RUTURAJ G.KALMEGH ❤️❤️❤️
RUTURAJ G.KALMEGH ❤️❤️❤️ - avatar
- 1
Why did you use /n in your code
22nd Jan 2021, 4:50 PM
Mohamed Ahmed Abdel Atty
Mohamed Ahmed Abdel Atty - avatar
- 1
And why the results are vertical
22nd Jan 2021, 4:51 PM
Mohamed Ahmed Abdel Atty
Mohamed Ahmed Abdel Atty - avatar
- 1
So why we used if in my code
22nd Jan 2021, 5:08 PM
Mohamed Ahmed Abdel Atty
Mohamed Ahmed Abdel Atty - avatar
- 1
And why we wrote lst[i]
22nd Jan 2021, 5:11 PM
Mohamed Ahmed Abdel Atty
Mohamed Ahmed Abdel Atty - avatar
- 1
What proper
22nd Jan 2021, 5:15 PM
Mohamed Ahmed Abdel Atty
Mohamed Ahmed Abdel Atty - avatar