10 python one-liners to save your time
1 min readDec 31, 2022
Here is a list of 10 python one-liners that saved a lot of my time while coding in Python.
- Find the maximum value in a list:
max_value = max([1, 2, 3, 4, 5])
2. Find the minimum value in a list:
min_value = min([1, 2, 3, 4, 5])
3. Reverse a string:
reversed_string = "Hello World"[::-1]
4. Concatenate a list of strings into a single string:
concatenated_string = " ".join(["Hello", "World"])
5. Check if a string contains a substring:
string_contains_substring = "Hello" in "Hello World"
6. Check if a list contains a value:
list_contains_value = 5 in [1, 2, 3, 4, 5]
7. Sort a list in ascending order:
sorted_list = sorted([5, 2, 4, 1, 3]
8. Sort a list in descending order:
sorted_list = sorted([5, 2, 4, 1, 3], reverse=True)
9. Create a dictionary from a list of tuples:
dictionary = dict([("a", 1), ("b", 2), ("c", 3)]
10. Create a list of the keys in a dictionary:
keys = list(dictionary.keys())
I hope these one-liners will save you time and make your Python code more concise!