(Python)Implement the function deep_list, which takes in a list, and returns a new list which contains only elements of the original list that are also lists. Use a list comprehension.
def deep_list(seq): ""
"Returns a new list containing elements of the original list that are lists.
>>> seq = [49, 8, 2, 1, 102]
>>> deep_list(seq) []
>>> seq = [[500], [30, 25, 24], 8, [0]]
>>> deep_list(seq) [[500], [30, 25, 24], [0]]
>>> seq = ["hello", [12, [25], 24], 8, [0]]
>>> deep_list(seq) [[12, [25], 24], [0]] """ "
*** YOUR CODE HERE ***"
def deep_list(seq): return [x for x in seq if type(x)==list] # Testing print(deep_list([49, 8, 2, 1, 102])) print(deep_list([[500], [30, 25, 24], 8, [0]])) print(deep_list(["hello", [12, [25], 24], 8, [0]]))
(Python)Implement the function deep_list, which takes in a list, and returns a new list which contains...
Python. Write a function that takes in a list and returns the first nonzero entry. def nonzero(lst): """ Returns the first nonzero element of a list >>> nonzero([1, 2, 3]) 1 >>> nonzero([0, 1, 2]) 1 >>> nonzero([0, 0, 0, 0, 0, 0, 5, 0, 6]) 5 """ "*** YOUR CODE HERE ***"
Implement a Python function called revd that takes a chain of characters and returns a new chain with the double of the elements but in reverse order. In the main program, ask the user to input the chain of characters, call the function and display the result Example : Please enter a chain of characters: abcd ddccbbaa NOTE: This is a repost. Please DO NOT use GLOBAL Variables
Python Write a function list_copy(l) that takes a list as a parameter and returns a copy of the list using a list comprehension. please provide unittest
For my question, I need to write a function in Python 3 called middle that takes a list and returns a new list that contains all elements but the first and last elements. After the implement this function, I need to pass lists to test the functionality of the middle function Prototype: middle(list)
implement function 2 in python. name the source file as h1.py. A test file h1_test.py is provided below! h1_test.py import re import math import pytest from h1 import (change, strip_quotes, scramble, say, triples, powers) def test_change(): assert change(0) == (0, 0, 0, 0) assert change(97) == (3, 2, 0, 2) assert change(8) == (0, 0, 1, 3) assert change(250) == (10, 0, 0, 0) assert change(144) == (5, 1, 1, 4) assert change(97) == (3, 2, 0, 2) assert change(100000000000)...
def zigzag(text): Implement a function that takes a string and returns a new string that is a zig-zag rearrangement of it like the one in the examples below. • Parameters: text is a string (no validation needed) • Examples: zigzag('12345678') → '18273645' zigzag('123456789') → '192837465' zigzag('abcdefgh') → 'ahbgcfde' zigzag('GeorgeMason') → 'GneoosragMe' in python
2. Consider the following function # listOfNumbers is a list of only numbers # def processList(listOfNumbers): result = [] for i in listOfNumbers: if i<0 == 0: result.append(i*i) else: result.append((i*i)+1) return result First, study and test processList(listOfNumbers) to determine what it does Then rewrite its body so that it accomplishes the same task with a one-line list comprehension. Thus, the resulting function will have exactly two lines, the def line and a return line containing a list comprehension expression. 3....
Hello can anyone help solve this please in Python the way it's asked? Question 22 40 pts List Comprehensions Write the solution to a file named p2.py and upload it here. Comprehensions will show up at the midterm exam, so it's important to practice. a) Write a list comprehension that returns a list [0, 1,4, 9, 16, .. 625] starting from range(0, 26). (lots of list elements were omitted in ...) b) Write a list comprehension that returns the list...
1 write a Python function that takes in a list of integers and returns maximum and minimum values in the list as a tuple. Hint (can be done in one pass, you are not allowed to use built-on min and max functions.)max, min = find_max_min(my_list):2 write a Python function that takes in a list of integers (elements), and an integer number (num). The functions should count and return number of integers in elements greater than, less than, and equal to...
use python 2a. Union (1 points) Make a union function which takes in 2 lists as parameters and returns a list. It should return a list that contain all elements that are in either list. Your results list should contain no duplicates. For example, if the two lists are [1,5,6,5, 2] and [3,5, 1,9] then the result list is (1,5,6,2,3,9). Note that the list sizes don't have to be equal. Note that the input lists could have duplicates, but your...