
reverse string
1 2 3
| a = 'string' print ("Reverse is", a[::-1])
|
cast list value for each variable
1 2 3 4
| a = [1, 2, 3] x, y, z = a print(x, y, z)
|
Join list
1 2 3
| a = ["Code", "Python", "Developer"] > print " ".join(a)
|
loop 2 list in 1 for command
1 2 3 4 5 6 7 8 9
| list1 = ['a', 'b', 'c', 'd'] list2 = ['p', 'q', 'r', 's'] for x, y in zip(list1,list2): print x, y
|
swap variable 1 line
1 2 3 4 5
| a=7 b=5 b, a = a, b print(a, b)
|
js can do follow this style:
1 2 3
| var a = 5, b= 7 [b, a] = [a, b] console.log(a, b)
|
Repeat string
1 2
| print('code'*4+' '+'mentor'*5)
|
Convert 2D list -> 1D list not loop
1 2 3 4
| a = [[1, 2], [3, 4], [5, 6]] import itertools print(list(itertools.chain.from_iterable(a)))
|
1 2 3 4
| > result = map(lambda x:int(x) ,raw_input().split()) 1 2 3 4 > result [1, 2, 3, 4]
|
cast string to list number use map
1 2 3 4
| s = '1 2 3 4' result = map(int, s.split()) print(result)
|