❤3
Forwarded from Machine Learning with Python
reversed() in Python - what supports it and what doesn'tThe function
reversed() is built-in in Python, but it doesn't work with all data types✓ Lists - it works
reversed([1, 2, 3]) returns an iteratorlist(reversed([1, 2, 3])) → [3, 2, 1]✓ Tuples - it also works
reversed((1, 2, 3)) can be easily iterated✗ Sets - not supported
reversed({1, 2, 3}) → TypeErrorWhy? Sets don't have a fixed order, so they can't be "reversed"
If you need to reverse a set:
list(reversed(list({1, 2, 3})))A bit of #Python basics. Day 8 - Flatten a nested list
I'll show you three (3) ways to flatten a two-dimensional list. The first method uses a
⚙️ Using a
For this method, we use a nested
⚙️ Using the itertools module:
The
You can see that the nested loop has been flattened.
⚙️ Using list comprehension
If you don't want to import
List comprehension is well suited for moderately nested lists. For deeply nested lists, it is not suitable, as the code becomes harder to read.
⚙️ Using a generator function
You can create a generator function that yields elements from the nested list, and then convert the generator into a list.
The generator method is suitable for flattening large or deeply nested lists. This is because generators are memory-efficient.
👉 https://news.1rj.ru/str/DataScience4
I'll show you three (3) ways to flatten a two-dimensional list. The first method uses a
for loop, the second uses the itertools module, and the third uses list comprehension.for loop:For this method, we use a nested
for loop. The outer loop iterates over the inner lists, and the inner loop accesses the elements in the inner lists.# In [19]:
list1 = [[1, 2, 3],[4, 5, 6]]
newlist = []
for list2 in list1:
for j in list2:
newlist.append(j)
print(newlist)[1, 2, 3, 4, 5, 6]The
itertools.chain.from_iterable() function from the itertools module can be used to flatten a nested list. This method may not be suitable for deeply nested lists.# In [20]:
import itertools
list1 = [[1, 2, 3],[4, 5, 6]]
flat_list = list(itertools.chain.from_iterable(list1))
print(flat_list)[1, 2, 3, 4, 5, 6]You can see that the nested loop has been flattened.
If you don't want to import
itertools or write a regular for loop, you can simply use list comprehension.# In [21]:
list1 = [[1, 2, 3], [4, 5, 6]]
flat_list = [i for j in list1 for i in j]
print(flat_list)[1, 2, 3, 4, 5, 6]List comprehension is well suited for moderately nested lists. For deeply nested lists, it is not suitable, as the code becomes harder to read.
You can create a generator function that yields elements from the nested list, and then convert the generator into a list.
# In [22]:
def flatten_generator(nested_list):
for sublist in nested_list:
for item in sublist:
yield item
list1 = [[1, 2, 3], [4, 5, 6]]
flat_list = list(flatten_generator(list1))
flat_list
Out[22]: [1, 2, 3, 4, 5, 6]The generator method is suitable for flattening large or deeply nested lists. This is because generators are memory-efficient.
Please open Telegram to view this post
VIEW IN TELEGRAM
Telegram
Code With Python
This channel delivers clear, practical content for developers, covering Python, Django, Data Structures, Algorithms, and DSA – perfect for learning, coding, and mastering key programming skills.
Admin: @HusseinSheikho || @Hussein_Sheikho
Admin: @HusseinSheikho || @Hussein_Sheikho
❤6
✨ Kilo Code | AI Coding Tools ✨
📖 An open-source AI coding agent for VS Code, JetBrains, and the command line with support for over 500 AI models.
🏷️ #Python
📖 An open-source AI coding agent for VS Code, JetBrains, and the command line with support for over 500 AI models.
🏷️ #Python
✨ Quiz: The pandas DataFrame: Make Working With Data Delightful ✨
📖 Test your pandas skills! Practice DataFrame basics, column access, creation, sorting, and data manipulation in this interactive quiz.
🏷️ #intermediate #data-science
📖 Test your pandas skills! Practice DataFrame basics, column access, creation, sorting, and data manipulation in this interactive quiz.
🏷️ #intermediate #data-science
❤1
✨ How to Use Note-Taking to Learn Python ✨
📖 Having a hard time retaining information from learning resources? Learn some Python note-taking tips to enhance your learning experience!
🏷️ #basics
📖 Having a hard time retaining information from learning resources? Learn some Python note-taking tips to enhance your learning experience!
🏷️ #basics
Absolute value (module) of a number
Let's say you have a negative number and you want to get its absolute value. For this, you can use the
Also,
➡️ Using the math module
If you need more advanced mathematical functions, you can use
➡️ Using a lambda function
You can also use
Let's say you have a negative number and you want to get its absolute value. For this, you can use the
abs() function. The abs() function returns the absolute value of any number (positive, negative, and complex). Below is shown how to get a list of absolute values from a list that contains both negative and positive numbers. We use list comprehension.list1 = [-12, -45, -67, -89, 34, 67, -13]
print([abs(num) for num in list1])
[12, 45, 67, 89, 34, 67, 13]Also,
abs() can be applied to a floating-point number, and it will return the absolute value. See below:num = -23.12
print(abs(num))
23.12If you need more advanced mathematical functions, you can use
fabs() from the math module. This function always returns a float.import math
num = -23.12
absolute_value = math.fabs(num)
absolute_value
23.12You can also use
lambda to turn a negative number into its absolute value. The code below checks if x is less than zero (that is, if it's a negative value). If so, it returns -x, essentially removing the minus and making the number positive. If x is not negative (greater than or equal to 0), it returns x as it is.num = -23.12
absolute_value = (lambda x: -x if x < 0 else x)(num)
absolute_value
23.12
Please open Telegram to view this post
VIEW IN TELEGRAM
1❤3
✨ base64 | Python Standard Library ✨
📖 A Python standard library module for encoding binary data as ASCII text using Base16, Base32, Base64, and Base85 schemes.
🏷️ #Python
📖 A Python standard library module for encoding binary data as ASCII text using Base16, Base32, Base64, and Base85 schemes.
🏷️ #Python
✨ Quiz: Duck Typing in Python: Writing Flexible and Decoupled Code ✨
📖 Check your grasp of Python's duck typing. Recognize behavior-based interfaces, use protocols and special methods, and know alternatives. Try the quiz.
🏷️ #intermediate #python
📖 Check your grasp of Python's duck typing. Recognize behavior-based interfaces, use protocols and special methods, and know alternatives. Try the quiz.
🏷️ #intermediate #python
✨ Quiz: Automate Python Data Analysis With YData Profiling ✨
📖 Test your knowledge of YData Profiling, including report creation, customization, performance optimization, time series analysis, and comparisons.
🏷️ #intermediate #data-science #data-viz
📖 Test your knowledge of YData Profiling, including report creation, customization, performance optimization, time series analysis, and comparisons.
🏷️ #intermediate #data-science #data-viz
❤1
Forwarded from Machine Learning with Python
Over 20 free courses are now available on our channel for a very limited time.
https://news.1rj.ru/str/DataScienceC
https://news.1rj.ru/str/DataScienceC
Telegram
Udemy Coupons
ads: @HusseinSheikho
The first channel in Telegram that offers free
Udemy coupons
The first channel in Telegram that offers free
Udemy coupons
❤1
✨ bdb | Python Standard Library ✨
📖 A Python standard library module that provides a generic debugger framework for setting breakpoints, stepping through code, and managing trace events.
🏷️ #Python
📖 A Python standard library module that provides a generic debugger framework for setting breakpoints, stepping through code, and managing trace events.
🏷️ #Python
✨ Quiz: How to Use the OpenRouter API to Access Multiple AI Models via Python ✨
📖 Test your Python skills with OpenRouter: learn unified API access, model switching, provider routing, and fallback strategies.
🏷️ #intermediate #ai #api
📖 Test your Python skills with OpenRouter: learn unified API access, model switching, provider routing, and fallback strategies.
🏷️ #intermediate #ai #api
🚀 Master Data Science & Programming!
Unlock your potential with this curated list of Telegram channels. Whether you need books, datasets, interview prep, or project ideas, we have the perfect resource for you. Join the community today!
🔰 Machine Learning with Python
Learn Machine Learning with hands-on Python tutorials, real-world code examples, and clear explanations for researchers and developers.
https://news.1rj.ru/str/CodeProgrammer
🔖 Machine Learning
Machine learning insights, practical tutorials, and clear explanations for beginners and aspiring data scientists. Follow the channel for models, algorithms, coding guides, and real-world ML applications.
https://news.1rj.ru/str/DataScienceM
🧠 Code With Python
This channel delivers clear, practical content for developers, covering Python, Django, Data Structures, Algorithms, and DSA – perfect for learning, coding, and mastering key programming skills.
https://news.1rj.ru/str/DataScience4
🎯 PyData Careers | Quiz
Python Data Science jobs, interview tips, and career insights for aspiring professionals.
https://news.1rj.ru/str/DataScienceQ
💾 Kaggle Data Hub
Your go-to hub for Kaggle datasets – explore, analyze, and leverage data for Machine Learning and Data Science projects.
https://news.1rj.ru/str/datasets1
🧑🎓 Udemy Coupons | Courses
The first channel in Telegram that offers free Udemy coupons
https://news.1rj.ru/str/DataScienceC
😀 ML Research Hub
Advancing research in Machine Learning – practical insights, tools, and techniques for researchers.
https://news.1rj.ru/str/DataScienceT
💬 Data Science Chat
An active community group for discussing data challenges and networking with peers.
https://news.1rj.ru/str/DataScience9
🐍 Python Arab| بايثون عربي
The largest Arabic-speaking group for Python developers to share knowledge and help.
https://news.1rj.ru/str/PythonArab
🖊 Data Science Jupyter Notebooks
Explore the world of Data Science through Jupyter Notebooks—insights, tutorials, and tools to boost your data journey. Code, analyze, and visualize smarter with every post.
https://news.1rj.ru/str/DataScienceN
📺 Free Online Courses | Videos
Free online courses covering data science, machine learning, analytics, programming, and essential skills for learners.
https://news.1rj.ru/str/DataScienceV
📈 Data Analytics
Dive into the world of Data Analytics – uncover insights, explore trends, and master data-driven decision making.
https://news.1rj.ru/str/DataAnalyticsX
🎧 Learn Python Hub
Master Python with step-by-step courses – from basics to advanced projects and practical applications.
https://news.1rj.ru/str/Python53
⭐️ Research Papers
Professional Academic Writing & Simulation Services
https://news.1rj.ru/str/DataScienceY
━━━━━━━━━━━━━━━━━━
Admin: @HusseinSheikho
Unlock your potential with this curated list of Telegram channels. Whether you need books, datasets, interview prep, or project ideas, we have the perfect resource for you. Join the community today!
Learn Machine Learning with hands-on Python tutorials, real-world code examples, and clear explanations for researchers and developers.
https://news.1rj.ru/str/CodeProgrammer
Machine learning insights, practical tutorials, and clear explanations for beginners and aspiring data scientists. Follow the channel for models, algorithms, coding guides, and real-world ML applications.
https://news.1rj.ru/str/DataScienceM
This channel delivers clear, practical content for developers, covering Python, Django, Data Structures, Algorithms, and DSA – perfect for learning, coding, and mastering key programming skills.
https://news.1rj.ru/str/DataScience4
Python Data Science jobs, interview tips, and career insights for aspiring professionals.
https://news.1rj.ru/str/DataScienceQ
Your go-to hub for Kaggle datasets – explore, analyze, and leverage data for Machine Learning and Data Science projects.
https://news.1rj.ru/str/datasets1
The first channel in Telegram that offers free Udemy coupons
https://news.1rj.ru/str/DataScienceC
Advancing research in Machine Learning – practical insights, tools, and techniques for researchers.
https://news.1rj.ru/str/DataScienceT
An active community group for discussing data challenges and networking with peers.
https://news.1rj.ru/str/DataScience9
The largest Arabic-speaking group for Python developers to share knowledge and help.
https://news.1rj.ru/str/PythonArab
Explore the world of Data Science through Jupyter Notebooks—insights, tutorials, and tools to boost your data journey. Code, analyze, and visualize smarter with every post.
https://news.1rj.ru/str/DataScienceN
Free online courses covering data science, machine learning, analytics, programming, and essential skills for learners.
https://news.1rj.ru/str/DataScienceV
Dive into the world of Data Analytics – uncover insights, explore trends, and master data-driven decision making.
https://news.1rj.ru/str/DataAnalyticsX
Master Python with step-by-step courses – from basics to advanced projects and practical applications.
https://news.1rj.ru/str/Python53
Professional Academic Writing & Simulation Services
https://news.1rj.ru/str/DataScienceY
━━━━━━━━━━━━━━━━━━
Admin: @HusseinSheikho
Please open Telegram to view this post
VIEW IN TELEGRAM
❤3
✨ Python Gains frozendict and Other Python News for March 2026 ✨
📖 Catch up on the latest Python news: frozendict joins the built-ins, Django patches SQL injections, and AI SDKs race to add WebSocket transport.
🏷️ #community #news
📖 Catch up on the latest Python news: frozendict joins the built-ins, Django patches SQL injections, and AI SDKs race to add WebSocket transport.
🏷️ #community #news
✨ MLflow | AI Coding Tools ✨
📖 An open-source platform for managing the machine learning lifecycle, including experiment tracking, model packaging, registry management, and GenAI observability.
🏷️ #Python
📖 An open-source platform for managing the machine learning lifecycle, including experiment tracking, model packaging, registry management, and GenAI observability.
🏷️ #Python
✨ Quiz: Python Stacks, Queues, and Priority Queues in Practice ✨
📖 Test your knowledge of stacks, queues, deques, and priority queues with practical questions and Python coding exercises.
🏷️ #intermediate #algorithms #data-structures
📖 Test your knowledge of stacks, queues, deques, and priority queues with practical questions and Python coding exercises.
🏷️ #intermediate #algorithms #data-structures