Python filter none from list | Example code

With Python, the filter function can check for any None value in the list and remove them and form a filtered list without the None values. Python filter(None) from list example code Simple example code. Output: Remove None value from a list without removing the 0 value A list comprehension is likely the cleanest way: Read More…


List comprehension nested for loop | Example code

You can write more fast and compact code using the list compression and nested loop in Python. OR Example List comprehension nested for loop Simple example code uses two for loops in list Comprehension and the final result would be a list of lists. we will not include the same numbers in each list. we Read More…


Python subtract lists element by element | Example code

Use zip() to Python subtract lists element by element in Python. Example subtract lists element by element in Python Simple example code uses a for-loop to iterate over the zip object and subtract the lists’ elements from each other and store the result in a list. Output: Anothe example Simple Python code to subtract if Read More…


Python list of objects to JSON | Example code

You can use a list comprehension to produce a list of dictionaries, then convert that using json.dumps() function. Convert Python list of objects to JSON example Simple example codes a dump method in your object and uses it when sending things to json module: Output: Do comment if you have any doubts and suggestions on Read More…


Convert Python list to JSON array | Example code

Use json.dumps() to convert a list to a JSON array in Python. The dumps() function takes a list as an argument and returns a JSON String. Python list to JSON array example Simple example code converting given list [“1”, “2”, “3”] to a JSON array results in the string ‘[“1”, “2”, “3”]’. For this example, Read More…


Subtract two lists Python | Example code

Use the zip() function to subtract two lists in Python. You have to also use for-loop to iterate over the zip object and subtract the lists’ elements from each other and store the result in a new list. Example Subtract two lists Python Simple example code. Output: Another example with using list comprehensions Example list Read More…


List attributes Python | Basics

The list has many attributes and methods that you can use. Here are attributes of the list in Python: list.append(x) # append x to end of list list.extend(iterable) # append all elements of iterable to list list.insert(i, x) # insert x at index i list.remove(x) # remove first occurance of x from list list.pop([i]) # Read More…


Define Global list in Python | Example code

You can declare Global list on the file/module level like: my_global_list = list() in Python. If you want to append to it inside a function you can use the global keyword. Example global list in Python Simple example code. Output: Another example Declare a global list and use it in function. Output: [1, 3] Do Read More…


Convert Tuple to list Python | Example code

Use the built-in list() function to convert a tuple to a list in Python. Simply passes the given tuple as an argument to the function. Example Tuple to list Python Simple example code initializes a tuple and converts it to a list using list(sequence). Output: Another Example The same way you can convert Tuple of Read More…


Python compare two lists of strings | Examples

Python doesn’t have a direct method to compare a list. But there are multiple ways to compare the two lists of strings in python. The following methods to perform List comparison: reduce() and map() functions collection.counter() method sort() method and == operator set() method and == operator Custom List Comprehension Note: The cmp() function doesn’t Read More…