Python
nested_lists = [[1,2,3], [4,5,6], [7,8,9]] printout each single item in the above nested lists
[[print(x) for x in l] for l in nested_lists]
Create answer which returns only consonant in 'amazing' using list comprehension.
answer = [x for x in 'amazing' if x in 'aeiou']
Using list comprehensions : ['O', 'X', 'O']
board = ['X' if x % 2 == 0 else 'O' for x in range(1,4)]
Using list comprehensions : [['O', 'X', 'O'], ['O', 'X', 'O'], ['O', 'X', 'O']]
board = [['X' if x % 2 == 0 else 'O' for x in range(1,4)] for val in range(1,4)]
Generate board as shown : [[1, 2, 3], [1, 2, 3], [1, 2, 3]] using list comprehensions
board = [[num for num in range(1,4)] for val in range(1,4)]
num_1 = [1,2,3,4,5] num_2 = [3,4,5,6,7] using list comprehension write code for creating a list of overlapping values
comb_nums = [x for x in num_1 if x in num_2]
nums = [1,2,3,4,5,6] Using list comprehensions print out an odds and even list.
evens = [x for x in nums if x % 2 == 0] odds = [x for x in nums if x % 2 != 0]
Using list comprehensions , print out Boolean value for the list = [1,0, 'x', None]
list_1 = [1,0, 'x', None] list_2 = [bool(x) for x in list_1] print(list_2)
with_vowels = 'This is so much fun' using list comprehension create a list f letters without vowels. Join the list consonants together intersperse list letters with x's
list_1 = [x for x in with_vowels if x not in 'aeiou'] joined_list = ' '.join(list_1) x_list = 'x'.join(list_1)
Using list comprehensions , and range 1-5 print out [10,20,30,40,50].
list_1 = [x*10 for x in range(1,6]] print(list_1)
name = 'tom' Using list comprehensions print out ['T', 'O', 'M']
name = 'tom' name_upper = [x.upper() for x in name] print(name_upper)
names = ['Ellie', 'Tim', 'Matt'] return : ['eille', 'mit', 'ttam']
names_1 = [x[::-1].lower() for x in names]
Using list comprehension convert a list of numbers to a list of numbers that are string types.
numbers = [1,2,3,4,5,6] string_numbers = [str(x) for x in numbers] print(string_numbers)
numbers = list of numbers 1-100 divisible by 12
numbs = [x for x in range(1,100) if x % 12 ==0]
nums = [1,2,3,4,5,6] using list comprehensions print out 'odds' and even numbers - in one line of code. e.g['odd', 2, 'odd' 4]
nums = [1,2,3,4,5,6,7] list_1 = [x if x % 2 == 0 else 'odd' for x in nums]
nums = [1,2,3,4] Using list Comprehensions print out list with [10,20,30,40]
nums = [1,2,3,4] list_1 = [x*10 for x in nums] print(list_1)