Sorting isn't just ascending or descending—sorted can use custom keys to unlock advanced functionality! #PythonTricks #pythonprogramming ---- # Sort by string length names = ["Alice", "Bob", "Christina", "Daniel"] sorted_names = sorted(names, key=len) print(sorted_names) ----
Enumerate can be used to tracking indices—with custom starting points. #PythonTricks #python #pythonprogramming --- # Enumerate with a custom start index my_list = ['Python', 'Rocks', 'Hard'] for index, value in enumerate(my_list, start=100): print(f"{index}: {value}") ---
Python's zip for combining list and split lists back apart. --- list1=[1, 2, 3] list2=['a', 'b', 'c'] # Combine zipped = list(zip(list1, list2)) print(zipped) # Split unzipped = zip(*zipped) list1_split, list2_split = map(list, unzipped) print(list1_split, list2_split) ---
We can flatten lists with this one-liner for clean, readable code. #PythonTricks #python #pythonprogramming #pythonlearning . ---- # Flatten a 2D list into 1D matrix = [[1, 2], [3, 4], [5, 6]] flat_list = [item for sublist in matrix for item in sublist] print(flat_list) ----
We can get rid of KeyError! defaultdict lets you set default values for missing keys easily. #PythonTricks #python #pythonprogramming #coding ---- from collections import defaultdict scores = defaultdict(int) scores['Python'] += 1 scores['Data Science'] += 2 print(scores) ----
You can use itertools for infinite iteration. Repeat values or generate infinite sequences #PythonTricks #PythonTips #python #coding #pythonprogramming from itertools import cycle colors = cycle(['red', 'green', 'blue']) for _ in range(5): print(next(colors))
You can use conditionals in list comprehensions. Compactly filter and process data #PythonTricks #PythonTips #python #coding #pythonprogramming #datascience nums = [1, 2, 3, 4, 5] squared_evens = [n**2 for n in nums if n % 2 == 0] print(squared_evens)
Need quick counts? Use collections.Counter Easily count elements in a list or string #PythonTricks #PythonTips #python #coding #pythonprogramming #datascience from collections import Counter data = ['apple', 'banana', 'apple'] counts = Counter(data) print(counts)
Simplify parallel iteration with zip(). Easily combine two or more iterables. #PythonTricks #PythonTips #python #coding #pythonprogramming #datascience names = ['Alice', 'Bob'] scores = [95, 88] for name, score in zip(names, scores): print(f'{name} scored {score}')
F-Strings for dynamic code execution. They are not just for formatting. It can b be used to evaluate expressions too! #PythonTricks #PythonTips #python #coding #pythonprogramming #datascience name = 'Python' print(f'{name.upper()} is AWESOME!') # Output: PYTHON is AWESOME!
Meet the Walrus Operator (:=) Save intermediate results in expressions #PythonTricks #PythonTips #python #coding #pythonprogramming #datascience nums = [10, 20, 30] if (total := sum(nums)) > 50: print(f'Sum is {total}, which is big!')
Python Question / Quiz; What is the output of the following Python code, and why? 🤔 Comment your answers below! #pythonlearning #pythonquiz #python
Python Question / Quiz; What is the output of the following Python code, and why? 🤔 Comment your answers below! #pythonlearning #pythonquiz #python
Python Question / Quiz; What is the output of the following Python code, and why? 🤔 Comment your answers below! #pythonlearning #pythonquiz #python
Python Question / Quiz; What is the output of the following Python code, and why? 🤔 Comment your answers below! #pythonlearning #pythonquiz #python
Python Question / Quiz; What is the output of the following Python code, and why? 🤔 Comment your answers below! #pythonlearning #pythonquiz #python
Python Question / Quiz; What is the output of the following Python code, and why? 🤔 Comment your answers below! #pythonlearning #pythonquiz #python
Python Question / Quiz; What is the output of the following Python code, and why? 🤔 Comment your answers below! #pythonlearning #pythonquiz #python
“Messi is so humble” SMH
United States Trends
- 1. #AEWFullGear 22,3 B posts
- 2. $CUTO 4.863 posts
- 3. Kansas 33,4 B posts
- 4. Colorado 60,9 B posts
- 5. Devin Neal 5.863 posts
- 6. Arizona State 8.829 posts
- 7. #BeatTheSnail N/A
- 8. Big 12 18,6 B posts
- 9. Ryan Williams N/A
- 10. Notre Dame 19 B posts
- 11. Penn State 10,5 B posts
- 12. Ole Miss 34,1 B posts
- 13. Cam Coleman N/A
- 14. Travis Hunter 11,4 B posts
- 15. Heisman 9.783 posts
- 16. #MostRequestedLive 11,1 B posts
- 17. Indiana 64,1 B posts
- 18. Kenny Dillingham 1.078 posts
- 19. Nissan 25,1 B posts
- 20. Sanders 26,2 B posts
Something went wrong.
Something went wrong.