0% found this document useful (0 votes)
162 views

Python One Liners

This document provides examples of Python one-liners to perform common tasks like assigning variables, list comprehensions, conditional logic, mapping, merging lists into dictionaries, modifying dictionaries, and lambda functions. The one-liners demonstrate concise ways to work with collections, variables, and functions in a single line of Python code.

Uploaded by

Anthony Okpe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
162 views

Python One Liners

This document provides examples of Python one-liners to perform common tasks like assigning variables, list comprehensions, conditional logic, mapping, merging lists into dictionaries, modifying dictionaries, and lambda functions. The one-liners demonstrate concise ways to work with collections, variables, and functions in a single line of Python code.

Uploaded by

Anthony Okpe
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Python One-Liners

by Alex Kelin

prompt command result

>>> print(name,
Assigning multiple name, quantity, price = 'Book', 3, 4.99 quantity, price)
values to variables
Book 3 4.99

one = ['a', 1, 'b', 'b', 4, '1']


Conditional [] >>> print(common)
two = ['h', 'l', 1, 'a', 'j', '1']
comprehension ['a', 1, '1']
common = [x for x in one if x in two]

lib = [4,8,2,4,0,3] >>> print(double_nums)


[] comprehension
double_nums = [num * 2 for num in lib] [8, 16, 4, 8, 0, 6]

a = 1
Swapping two >>> print(a, b)
b = 2
variables 2 1
a, b = b, a

Conditional
one = [1, 2, 3, 4, 5, 6, 7] >>> print(new)
comprehension,
new = [x if x % 2 == 0 else x * 2 for x in one] [2, 2, 6, 4, 10, 6, 14]
ternery operator

one = ['a', 'b', 'c', 'd', 'e'] >>> print(two)


two = one[::-1] ['e', 'd', 'c', 'b', ‘a']
Reverse []
or >>> print(three)
three = ['a', 'b', 'c', 'd', 'e'][::-1] ['e', 'd', 'c', 'b', 'a']

>>> print(all(a))
a = [True, True, True] True
Check for False b = [True, True, False]
>>> print(all(b))
False

>>> print(a)
Printing elements a = [1, 2, ‘three', 4, 5] [1, 2, ‘three', 4, 5]
of a collection >>> print(*a)
1 2 three 4 5

Converting string a = '1 2 3 4 5' >>> print(b)


with numbers to b = list(map(int, a.split())) [1, 2, 3, 4, 5]
integer []

Find the Most a = [4, 1, 2, 2, 3, 3, 3] >>> print(most_frequent)


Frequent Element [] most_frequent = max(set(a), key=a.count) 3

>>> print(option_1)
[1, 'h', 4, 'a', 'b',
values = [‘h',1,'b','b',4,'1','a',4]
‘1’]
option_1 = list({x for x in values})
or >>> print(option_2)
option_2 = list(set(values)) [1, 'h', 4, 'a', 'b',
Unique values only or ‘1’]
[] option_3 = [x for x in set(values)] >>> print(option_3)
or [1, 'h', 4, 'a', 'b',
option_4 = [] ‘1']
[option_4.append(x) for x in values if x not in >>> print(option_4)
option_4]
['h', 1, 'b', 4, '1',
'a']

one = ['a', 'b', 'c', 'd', 'e', 'f', ‘g']


result = one[2:6:2] >>> print(result)
Traverse []
or ['c', ‘e']
result = [x for x in one[2:6:2]]

one = [1, 2, 3, 4, 5]
two = ['a', 'b', 'c', 'd', ‘e']
>>> print(hm)
Merge two lists hm = dict([(one[i], two[i]) for i in
into {:} {1: 'a', 2: 'b', 3: 'c',
range(len(one))]) 4: 'd', 5: 'e'}
or
hm = {one[i]: two[i] for i in range(len(one))}

old_stock = {'water': 1.42, 'cheese': 2.5, 'milk':


2.0} >>> print(correction)
Moderate {:} price = 0.76 {'water': 1.0792,
'cheese': 1.9, 'milk':
correction = {item: value*price for (item, value) 1.52}
in old_stock.items()}

Simple value >>> print(squared(5))


squared = lambda x: x ** 2
operation (λ fn) 25

Multiple value >>> print(value(2,1))


value = lambda x, y: x + 2 - y
operation (λ fn) 3

You might also like