🐍 Python Basics 3/6: Lists Slices and Lambdas
date
Mar 28, 2023
slug
python-basics-lists-slices-and-lambdas
status
Published
tags
Python Basics
summary
Learn about List Slices and Lambdas
type
Post
Last updated
Mar 30, 2023 12:44 AM
👋 What's up guys, today wee are gonna learn about
lists slices
and lists lambdas
, two quite advanced Python techniques. Yeah, I am showing you something advanced in a Basic tutorial, you'd better get used to this, because I will do it more times!!).But of course, I will not cover all aspects of this technique, but yes, just the necessary to you be able to walk by yourself. So let's go!
0) Lists Slices
If you are not used to the term,
Lists Slices
is the action to literally access elements inside a list, and yes, you may be wondering: is this so advanced? Are you serious?. Yes, I'm dead serious!! It's because Python has some different and better ways to access these elements when compared to others Programming Languages.#
# ---- Lists Slices ----
#
my_list = [2, 5, 8, 7, 9, 10]
first_elemeent = my_list[0]
last_element = my_list[-1]
pair_indexes_elements = my_list[0::2] # [start, end, steps]
1) Lists Lambdas
If you are used to the modern JavaScript, you probably stumbled upon with
arrow functions
and you quite get used to Lists Lambdas
faster!!Oh, never have heard about arrow functions? Don't worry, Lists Lambdas is way too easier to understand, behold!
#
# ---- Lists Lambdas ----
#
my_list = [2, 5, 8, 7, 9, 10]
pair_elements = [element for element in my_list if element % 2 == 0]
odd_elements = [element for element in my_list if element % 2 == 1]
power_elements = [element**2 for element in my_list]
2) Bonus
Range
is a built-in function that allows you to create simple arrows following the pattern bellow: [min_element, max_element, steps]
#
# ---- Range ----
#
# - creating a ranged list with min value equals to zero and max value equals to 100
# - also, the values will be each 3 numbers in a range between 0 - 100
my_ranged_list = range(0, 100, 3)
Hope you enjoyed! See ya at conditions and loops lesson! 👋