So, let's continue our learnings about the lists in python! Why and how to use list comprehension ? List is one of the most useful collection data type in python.It is a collection which is ordered and changeable.It Allows duplicate members. Many of us would have thought of what if we want to manipulate each and every list element? Suppose you are given a list of numbers and you want to add 1 to each element in your list: my_list = [1, 3, 3, 7] my_list_plus_one = my_list + 1 and if you have thought of some code like above then its not gonna work. You would get this error: TypeError : can only concatenate list (not "int") to list. Python thinks that you want to add a list and an integer together, which doesn’t make any sense. So how do we manipulate the list? The answer is using list comprehension syntax. Example: my_list_plus_one = [num + 1 for num in my_list] print(my_list_plus_one) # Output: [2, 4, 4, 8] Essentially...
Great work!!!
ReplyDeleteGreat work!!!
ReplyDeleteGreat work !
ReplyDeleteAmazing
ReplyDeleteHelpful!
ReplyDeleteInteresting information
ReplyDelete