Python Shorthand

reverse string

1
2
3
a =  'string'
print ("Reverse is", a[::-1])
#>Reverse is gnirts

cast list value for each variable

1
2
3
4
a = [1, 2, 3]
x, y, z = a
print(x, y, z)
#>1, 2, 3

Join list

1
2
3
a = ["Code", "Python", "Developer"]
> print " ".join(a)
#>Code Python Developer

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

#a p
#b q
#c r
#d s

swap variable 1 line

1
2
3
4
5
a=7
b=5
b, a = a, b
print(a, b)
#>5, 7

js can do follow this style:

1
2
3
var a = 5, b= 7
[b, a] = [a, b]
console.log(a, b)// 5 7

Repeat string

1
2
print('code'*4+' '+'mentor'*5)
#>codecodecodecode mentormentormentormentormentor

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, 5, 6]

Cast input to list

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)
#> [1, 2, 3, 4]