In Python, 1.You’re given the following list: x = [5, 2, 7, 11, 44, 256, 81, 0,
ID: 3741447 • Letter: I
Question
In Python,
1.You’re given the following list: x = [5, 2, 7, 11, 44, 256, 81, 0, 36]. Write a program that takes list x and makes a new list that has only the odd elements of this list in it. You should be able to write one line of code to solve it, but more lines is OK, too. 2.Write a function that takes a list of numbers (for example x = [5, 2, 7, 11, 44, 256, 81, 0, 36]) and makes a new list of only the first and last elements of the given list. Test your function. 3.Take a list, say for example, this one: x = [5, 2, 7, 11, 44, 256, 81, 0, 36] and write a program that prints out all the elements of the list that are less than 9. Instead of printing the elements one by one, make a new list that has all the elements less than 9 from this list in it and print out this new list. Write this in one line of Python, if you can figure out how.
Explanation / Answer
if __name__ == '__main__': x = [5, 2, 7, 11, 44, 256, 81, 0, 36] print(x) odd = [i for i in x if i % 2 == 1] #1 print(odd) firstLast = [x[0]] + [x[-1]] #2 print(firstLast) newList = [i for i in x if i < 9] #3 print(newList)Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.