Python list slicing
Step by step explanation of a "reverse a list" trick from previous post
Perhaps the most interesting operation you can do with lists is called slicing.
IT gives you opportunity to get portion of your list.
For example:
🔹 Step 1: Getting list slice
WE can also go through list from end to beginning, in this case, we use negative indexes:
🔹 Step 3: Introducing increment
We can also ad increment to slicing. In all previous examples increment was one, but what if we want to get every second element of the list?
🔹 Step 4: Omitting slicing parameters
As you can see, full slicing formula would be:
For example:
['a', 'b', 'c']
['d', 'e']
['a', 'c']
['a', 'b', 'c', 'd', 'e']
['e', 'd', 'c', 'b', 'a']
I put some effort into creating this so please let me know if you learned something valuable and if you like this type of content 😊.
➖➖➖➖➖➖➖➖➖➖➖➖➖➖
👉Join @python_bds for more👈
*This channel belongs to @bigdataspecialist group
Step by step explanation of a "reverse a list" trick from previous post
Perhaps the most interesting operation you can do with lists is called slicing.
IT gives you opportunity to get portion of your list.
For example:
🔹 Step 1: Getting list slice
x = ['a','b','c','d','e']
print(x[0]) #first element - a
print(x[0:1]) #first element, but we have explicitly set both start and end - ['a']
print(x[0:2]) #first two elements - ['a', 'b']
🔹 Step 2: Starting from the endWE can also go through list from end to beginning, in this case, we use negative indexes:
print(x[-3:-1]) # This will return the slice starting from the 3rd element from the end (c) and stopping before the 1st element from the end (e).Output: ['c', 'd']
🔹 Step 3: Introducing increment
We can also ad increment to slicing. In all previous examples increment was one, but what if we want to get every second element of the list?
print(x[0:5:2]) # starting from first element, ending with last one, with increment of 2Output: ['a', 'c', 'e']
🔹 Step 4: Omitting slicing parameters
As you can see, full slicing formula would be:
my_list[start_index:end_index:increment]But we can omit (leave out/exclude) any of those 3 parameters
For example:
print(x[:3]) # We omitted first parameter, this will print first 3 elements of the listOutput:
print(x[3:]) # this will print rest 2 elements (from 4th to end)
print(x[:3:2]) # this will print first 3 elements with increment 2
print(x[:]) # no start and end index, this will print entire list!
print(x[::-1]) # this will also print entire list, but with negative increment, starting from last to first one -> REVERSED LIST
['a', 'b', 'c']
['d', 'e']
['a', 'c']
['a', 'b', 'c', 'd', 'e']
['e', 'd', 'c', 'b', 'a']
I put some effort into creating this so please let me know if you learned something valuable and if you like this type of content 😊.
➖➖➖➖➖➖➖➖➖➖➖➖➖➖
👉Join @python_bds for more👈
*This channel belongs to @bigdataspecialist group
Telegram
Python Learning
Python tricks and tips
Section 1: Lists
Lesson 1: Reverse a list
Code snippet to copy:
a=[10,9,8,7]
print(a[::-1])
Section 1: Lists
Lesson 1: Reverse a list
Code snippet to copy:
a=[10,9,8,7]
print(a[::-1])
❤9🔥4👍2
Python Notes for Professionals book
📄 816 pages
🔗 Book link
#Python
➖➖➖➖➖➖➖➖➖➖➖➖➖
Join @python_bds for more
📄 816 pages
🔗 Book link
#Python
➖➖➖➖➖➖➖➖➖➖➖➖➖
Join @python_bds for more
👍4
Which type of Programming does Python support?
Anonymous Quiz
37%
object-oriented programming
4%
structured programming
3%
functional programming
56%
all of the mentioned
👍8🎉1
Is Python case sensitive when dealing with identifiers?
Anonymous Quiz
73%
yes
16%
no
9%
machine dependent
2%
none
👍4
Introduction to Python Programming
Course Notes
📄 164 pages
🔗 Book link
#Python
➖➖➖➖➖➖➖➖➖➖➖➖➖
Join @python_bds for more
Course Notes
📄 164 pages
🔗 Book link
#Python
➖➖➖➖➖➖➖➖➖➖➖➖➖
Join @python_bds for more
Is Python code compiled or interpreted?
Anonymous Quiz
44%
Python code is both compiled and interpreted
5%
Python code is neither compiled nor interpreted
9%
Python code is only compiled
41%
Python code is only interpreted
❤6
Python Frameworks and Libraries
Django is the most popular framework among Python developers. No surprise here, seeing how Web development is so popular among Python users. Interestingly, 43% of respondents are using IPython, which clearly shows it’s being used not only for scientific purposes, but for general software development as well.
A large number of respondents also specified Flask as their framework of choice.
Django is the most popular framework among Python developers. No surprise here, seeing how Web development is so popular among Python users. Interestingly, 43% of respondents are using IPython, which clearly shows it’s being used not only for scientific purposes, but for general software development as well.
A large number of respondents also specified Flask as their framework of choice.
👍6
Hi folks,
For all you who have been here for a long time, you probably noticed that after certain period of inactivity, this channel started posting content again. First owner of this channel (a friend of mine, admin of AI India), didn't have enough time to take care of it so he transferred ownership to me. I am 27 year old software engineer, living in Europe, owner and founder of @bigdataspecialist channel (and many more, i will add them in comment if somebody is interested into checking them).
I started posting interactive content, with one of my employees helping me with it since i don't have too much free time. I really hope you like it 😉
I would like to inform you that we are having giveaway tomorrow in my main channel (@bigdataspecialist). We are buying you paid course of your choice. Check last post in that channel for more info.
That's it, I really hope you like the new content, feel free to reach me out if you need anything and also write any suggestions what would you like to see in this or other channel of mine in comments. Thank you all and have a nice day.
For all you who have been here for a long time, you probably noticed that after certain period of inactivity, this channel started posting content again. First owner of this channel (a friend of mine, admin of AI India), didn't have enough time to take care of it so he transferred ownership to me. I am 27 year old software engineer, living in Europe, owner and founder of @bigdataspecialist channel (and many more, i will add them in comment if somebody is interested into checking them).
I started posting interactive content, with one of my employees helping me with it since i don't have too much free time. I really hope you like it 😉
I would like to inform you that we are having giveaway tomorrow in my main channel (@bigdataspecialist). We are buying you paid course of your choice. Check last post in that channel for more info.
That's it, I really hope you like the new content, feel free to reach me out if you need anything and also write any suggestions what would you like to see in this or other channel of mine in comments. Thank you all and have a nice day.
❤10