suppose list is
l
= [1,2,3,4,[1,2,3],(1,2),'a']
and you want to print all elements separated by space
output
= 1 2 3 4 [1, 2, 3] (1, 2) a
How can you do that ?
l = [1,2,3,4,[1,2,3],(1,2),'a']
print(*l)
import codewars_test as test
consider the following code !
l = [1, 2, 3, 4, 5]
a = []
for values in l:
if values==1:
a.append('yes')
elif values==2:
a.appned('no')
else:
a.append('idle')
print(a)
output : ['yes', 'no', 'idle', 'idle', 'idle']
How can you apply elif in list comprehenion ?
And what are the other ways to do this ?
l = [1, 2, 3, 4, 5]
a = ['yes' if v == 1 else 'no' if v == 2 else 'idle' for v in l]
print(a)
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(1 + 1, 2)
suppose there is list like
l = [[-44, 68], [-4, 108], [0, 112], [-48, 64]]
How can you extract all elements in a single list ?
answer is :
answer = [i for j in l for i in j]
print(asnwer)
output : [-44, 68, -4, 108, 0, 112, -48, 64]
l = [[-44, 68], [-4, 108], [0, 112], [-48, 64]]
answer = [i for j in l for i in j]
print(answer)
import codewars_test as test
# TODO Write tests
import solution # or from solution import example
# test.assert_equals(actual, expected, [optional] message)
@test.describe("Example")
def test_group():
@test.it("test case")
def test_case():
test.assert_equals(1 + 1, 2)
Suppose you want to generate a list like below
[1, 2, 2, 4, 3, 6, 4, 8, 5, 10, 6, 12, 7, 14, 8, 16, 9, 18]
you can observe pattern that its [i, i*2 for i in range(1,10)]
1 and 1 * 2 = 2
2 and 2 * 2 = 4 ... 9 and 9 * 2 = 18
But we can not execute [i, i*2 for i in range(1,10)]
so very simple solution is
a = []
for i in range(1,10):
a.append(i)
a.append(i*2)
but how can you do that in one line ?
lst_gen = list(sum([(i, i*2) for i in range(1, 10)],()))
print(lst_gen)
# TODO: Replace examples and use TDD by writing your own tests
# These are some of the methods available:
# test.expect(boolean, [optional] message)
# test.assert_equals(actual, expected, [optional] message)
# test.assert_not_equals(actual, expected, [optional] message)
# You can use Test.describe and Test.it to write BDD style test groupings