Python Tutorial: Slicing Lists and Strings

Corey Schafer · Beginner ·🛠️ AI Tools & Apps ·10y ago

Key Takeaways

This video tutorial by Corey Schafer covers the basics of slicing lists and strings in Python, including the use of positive and negative indexes, and the syntax for extracting subsets of elements from lists and strings.

Full Transcript

hey everybody how's it going in this python video we are going to learn how to perform slicing on our lists and our strings so if you don't know what slicing is it's a way for us to extract certain elements uh from these list and strings so for example here I have this uh my list here that is 0 1 2 3 4 5 6 7 8 9 now I have a comment underneath here that has all of the positive indexes so the index of zero is equal to the value of zero so down here where I'm printing out my list now this is probably a refresher for a lot of people but if we type in the index here then it's going to give us the value at that index so if I run that then you can see we get the value zero so if I was to type in the fifth index here then it should be the value five now what less people know about is that you can actually use negative indexes also and I have those listed in this comment as well so the1 index refers to the last value in the list which is nine so if I was to type in my list and got the Nega -1 index and printed that out you can see that we get the result nine here and if I was to go to the -10 index and print that out then you can see that we get the value zero okay so that's how we access single values in our list but how do we ACC uh how do we um extract a certain number or a range from this list well the way that we can do that is using this syntax that I have in this comment here so we can do a start value and an end value separated by a colon and then there's also this step value and we'll take a look at that here in a little bit so for example let's say that I wanted to get uh the values from the index of zero all the way to the index of five so I want 0o through five here here now the way that I'm going to do that is I'm going to type in my start index so I'm going to start at zero and then put in my colon now I'm going to put in my end index now the thing about the end is that it is non-inclusive so if I was to put a five here and print this out you can see that we get 0o through four because it gets to this fifth index and then it stops it doesn't include that so if I wanted to include that five then I would actually go put my end at as the six index and then print that out now you can see that I get 0 through five so just as another example here uh what if we wanted to get the values um three through 7even we wanted these values here so we could start at the index of three and we could go till the end index of eight because remember this is not inclusive so if I print that out you can see that we got 3 through 7 now we can also mix and match these positive and negative indexes so if I wanted three through seven I could also do7 which you can see here is the three and then I could go to -2 which you can see is the eight and if I print that out then it's the exact same thing and I said you could mix and match these positive and negative so if I wanted to go uh one through seven then I could do a positive index of one which is the value one here and go to two which is the eight which it will only print to the seven so if I save that and print it out then you can see that we got 1 through 7even now what if we wanted to print from one all the way to the end of the list here now remember the end is non- inclusive so if I put a nine in here that's not going to work because if I go one and then end at nine it doesn't include the nine value and it only prints out till the eight so really we can leave off values so if I left this nine off and just left this colon in then it's going to assume that I want to go to the very end so if I just do a one colon then it'll print from the index of one which is the value one all the way to the end of the list so if I print that out now you can see that I get one through nine so if I made this a five colon and then left this empty printed that out then I get 5 through n now this also works with the beginning index here for the start IND index so if I leave that out and I typed in a Nega one here then it'll go from 0o to eight and you can even leave both of these out and run it and you just get a copy of the entire list so so far we've only touched on this start and end index here we haven't really touched on this Step at all well the step allows us to skip a certain number of values so for example here if I wanted to start at the number two at the value two then I can start at index two and let's say I want to go to eight so I'll type in1 here because then it'll include the eight now if I print that out then we get 2 through 8 now the step will allow us to skip a certain number of values so if I do a step of two here then it'll print out every second value so if I run that then you can see that it printed out two 4 68 so what it did here is it started at index 2 which is this value of two here and it's going to negative one which is the nine uh but not inclusive so it's going to go to the eight and then it's going to print out every second value so it prints out the two Skips a value print out the four Skips a value six and so on Now the default step is just to print out every value it's a one so if I run that then it's the same as not even specifying the Step at at all now the step is interesting because you can actually do a negative step also which will run in Reverse now if I was just to make this a negative one right now and printed this out you can see that it is an empty array because what it's trying to do it's trying to go uh it's trying to print out 2 through eight but it has this negative step so there's just no way that it can get there like that but if I print out from -1 to two and I reverse those values and then print that out out now you can see that it started at the index of -1 and it's going to the index of two which is two and the step is1 so it's going from nine down to three and remember the end index is non inclusive even when you're going in reverse so now it's going from 9 to three so if I wanted to print uh from 8 to two in Reverse what I would have to do is I'd have to start at NE 2 which is the eight and I'd have to go to the index of one which is this one here and then a Nega One Step so if I run that you can see that we have um8 down to two now we can make this a -2 which will go in reverse and every other value so now we have 8642 and now lastly if we just wanted the entire list reversed then we can leave off the start so that it'll go completely to the beginning we can leave off the end so it'll go completely to the end and we can just negative step this by negative one so if I run that now you can see that we have the entire list in reverse and that's basically all there is to slicing uh lists in Python so U you can play around with this and try different values and see what you get now you can also slice uh strings so let's take a look at a couple of examples here where we can uh try to slice certain values out of a string so I have a sample URL here and I'm just going to print this UR URL out to the screen so now I have a couple of comments here so first of all uh let's see if we can reverse the URL so to reverse the URL then we did this the same thing with the list we can just start leave the start and end completely empty and then we can step by negative one which will go in reverse so now if I print this out you can see that we print out the sample URL up here and the reverse of that is here so now let's see if we can get just the top level domain from this URL so the top level domain here is this Doom uh so how would we slice that value out of that string I think the easiest way here would be to start from a negative value so I can start atga - 1 2 3 4 so if I type in -4 then I'm just going to go to the end of the string from -4 so if I print that out then you can see that we got now even though we're starting at4 our step is still a positive one by default so we're starting here and we're just going to the end of the URL which is the dot okay so now let's say for example that we want to print the URL without the beginning HTTP um so to do this we can start at zero 1 two 3 4 five six 7 so if we start at index s and then just go to the end of the of the string then it should give us the URL without the HTTP and the slashes so if I print that out now you can see that we just have the URL without the beginning HTTP and the slashes and now as a final example here let's print the URL without the HTTP or the top level domain and this kind of is just a combination of the two that we just did so we can start at index 7 and we can go till index -4 and that it will be non-inclusive so if we print that out you can see that it doesn't include that dot so now we only have the URL and it doesn't have the do or the HTTP at the beginning so that about does it for this video I hope after looking at all of these different examples you're now comfortable with all the different ways that you can perform slicing in Python but if you do have any questions feel free to ask in the comment section below um be sure to subscribe for future python videos and thank you all for watching

Original Description

In this video we will look at how to slice lists and strings in Python. Slicing allows us to extract certain elements from these lists and strings. This can be extremely useful for stripping out certain values from lists or getting a substring of a characters from a string. Let's take a look at a few code examples. The code from this video can be found at: https://github.com/CoreyMSchafer/code_snippets/tree/master/Slicing ✅ Support My Channel Through Patreon: https://www.patreon.com/coreyms ✅ Become a Channel Member: https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g/join ✅ One-Time Contribution Through PayPal: https://goo.gl/649HFY ✅ Cryptocurrency Donations: Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3 Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33 Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot ✅ Corey's Public Amazon Wishlist http://a.co/inIyro1 ✅ Equipment I Use and Books I Recommend: https://www.amazon.com/shop/coreyschafer ▶️ You Can Find Me On: My Website - http://coreyms.com/ My Second Channel - https://www.youtube.com/c/coreymschafer Facebook - https://www.facebook.com/CoreyMSchafer Twitter - https://twitter.com/CoreyMSchafer Instagram - https://www.instagram.com/coreymschafer/ #Python
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Corey Schafer · Corey Schafer · 0 of 60

← Previous Next →
1 Web fonts using CSS Font Face
Web fonts using CSS Font Face
Corey Schafer
2 Using Font Awesome in Desktop Applications (OS X)
Using Font Awesome in Desktop Applications (OS X)
Corey Schafer
3 Sublime Text 2: Setup, Package Control, and Settings
Sublime Text 2: Setup, Package Control, and Settings
Corey Schafer
4 ArcGIS API for JavaScript Part 1: Our First Web Map
ArcGIS API for JavaScript Part 1: Our First Web Map
Corey Schafer
5 Mac Tip: Windows' Snapping Feature on Mac with HyperDock
Mac Tip: Windows' Snapping Feature on Mac with HyperDock
Corey Schafer
6 Linux/Mac Terminal Tutorial: Creating Aliases for Commands
Linux/Mac Terminal Tutorial: Creating Aliases for Commands
Corey Schafer
7 ArcGIS API for JavaScript Part 2: Starting Templates
ArcGIS API for JavaScript Part 2: Starting Templates
Corey Schafer
8 Paver Patio Time Lapse
Paver Patio Time Lapse
Corey Schafer
9 Mac Tip: Ways to perform Screen Capturing and Screenshots
Mac Tip: Ways to perform Screen Capturing and Screenshots
Corey Schafer
10 WordPress Plugins: Imsanity
WordPress Plugins: Imsanity
Corey Schafer
11 WordPress Tips: Test your theme with Theme Unit Test and Monster Widget
WordPress Tips: Test your theme with Theme Unit Test and Monster Widget
Corey Schafer
12 Sublime Text 3: Setup, Package Control, and Settings
Sublime Text 3: Setup, Package Control, and Settings
Corey Schafer
13 Understanding Binary, Hexadecimal, Decimal (Base-10), and more
Understanding Binary, Hexadecimal, Decimal (Base-10), and more
Corey Schafer
14 Mac Tip: Adding Folder Stacks to the Dock
Mac Tip: Adding Folder Stacks to the Dock
Corey Schafer
15 CSS Tips and Tricks: Add External URLs to Print Stylesheets
CSS Tips and Tricks: Add External URLs to Print Stylesheets
Corey Schafer
16 JavaScript Arrays: Properties, Methods, and Manipulation (Part 7 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 7 of 7)
Corey Schafer
17 JavaScript Arrays: Properties, Methods, and Manipulation (Part 1 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 1 of 7)
Corey Schafer
18 JavaScript Arrays: Properties, Methods, and Manipulation (Part 5 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 5 of 7)
Corey Schafer
19 JavaScript Arrays: Properties, Methods, and Manipulation (Part 4 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 4 of 7)
Corey Schafer
20 JavaScript Arrays: Properties, Methods, and Manipulation (Part 3 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 3 of 7)
Corey Schafer
21 JavaScript Arrays: Properties, Methods, and Manipulation (Part 2 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 2 of 7)
Corey Schafer
22 JavaScript Arrays: Properties, Methods, and Manipulation (Part 6 of 7)
JavaScript Arrays: Properties, Methods, and Manipulation (Part 6 of 7)
Corey Schafer
23 Python Tutorial: if __name__ == '__main__'
Python Tutorial: if __name__ == '__main__'
Corey Schafer
24 Sublime Text Quick Tip: "Go To Definition" Click Shortcut
Sublime Text Quick Tip: "Go To Definition" Click Shortcut
Corey Schafer
25 How to quickly create favicons for the desktop, Apple/Android devices, tablets, and more
How to quickly create favicons for the desktop, Apple/Android devices, tablets, and more
Corey Schafer
26 Easily Resize Multiple Images Using Picasa
Easily Resize Multiple Images Using Picasa
Corey Schafer
27 Easily Resize Multiple Images Using the Mac Terminal
Easily Resize Multiple Images Using the Mac Terminal
Corey Schafer
28 Python Tutorial: virtualenv and why you should use virtual environments
Python Tutorial: virtualenv and why you should use virtual environments
Corey Schafer
29 Python Tutorial: pip - An in-depth look at the package management system
Python Tutorial: pip - An in-depth look at the package management system
Corey Schafer
30 Git Tutorial: Using the Stash Command
Git Tutorial: Using the Stash Command
Corey Schafer
31 How Software Engineers, Developers, and Designers can volunteer their skills
How Software Engineers, Developers, and Designers can volunteer their skills
Corey Schafer
32 Git Tutorial: Diff and Merge Tools
Git Tutorial: Diff and Merge Tools
Corey Schafer
33 Git Tutorial: Change DiffMerge Font-Size on Mac OSX
Git Tutorial: Change DiffMerge Font-Size on Mac OSX
Corey Schafer
34 Sublime Text Quick Tip: Launch Sublime Text from the Terminal
Sublime Text Quick Tip: Launch Sublime Text from the Terminal
Corey Schafer
35 Python Tutorial: str() vs repr()
Python Tutorial: str() vs repr()
Corey Schafer
36 Programming Terms: DRY (Don't Repeat Yourself)
Programming Terms: DRY (Don't Repeat Yourself)
Corey Schafer
37 Programming Terms: String Interpolation
Programming Terms: String Interpolation
Corey Schafer
38 Programming Terms: Idempotence
Programming Terms: Idempotence
Corey Schafer
39 Python Tutorial: Namedtuple - When and why should you use namedtuples?
Python Tutorial: Namedtuple - When and why should you use namedtuples?
Corey Schafer
40 Programming Terms: Mutable vs Immutable
Programming Terms: Mutable vs Immutable
Corey Schafer
41 Python Tutorial: Else Clauses on Loops
Python Tutorial: Else Clauses on Loops
Corey Schafer
42 Overview of Online Learning Resources
Overview of Online Learning Resources
Corey Schafer
43 Mac OS X Terminal Tutorial: Time-Saving Keyboard Shortcuts
Mac OS X Terminal Tutorial: Time-Saving Keyboard Shortcuts
Corey Schafer
44 Git Tutorial for Beginners: Command-Line Fundamentals
Git Tutorial for Beginners: Command-Line Fundamentals
Corey Schafer
45 Quickest and Easiest Way to Run a Local Web-Server
Quickest and Easiest Way to Run a Local Web-Server
Corey Schafer
46 Python Tutorial: Generators - How to use them and the benefits you receive
Python Tutorial: Generators - How to use them and the benefits you receive
Corey Schafer
47 Python Tutorial: Comprehensions - How they work and why you should be using them
Python Tutorial: Comprehensions - How they work and why you should be using them
Corey Schafer
48 Chrome Quick Tip: Quickly Bookmark Open Tabs for Later Viewing
Chrome Quick Tip: Quickly Bookmark Open Tabs for Later Viewing
Corey Schafer
49 Programming Terms: Combinations and Permutations
Programming Terms: Combinations and Permutations
Corey Schafer
50 Git Tutorial: Difference between "add -A", "add -u", "add .", and "add *"
Git Tutorial: Difference between "add -A", "add -u", "add .", and "add *"
Corey Schafer
51 Preparing for a Python Interview: 10 Things You Should Know
Preparing for a Python Interview: 10 Things You Should Know
Corey Schafer
52 SQL Tutorial for Beginners 1: Installing PostgreSQL and Creating Your First Database
SQL Tutorial for Beginners 1: Installing PostgreSQL and Creating Your First Database
Corey Schafer
53 SQL Tutorial for Beginners 2: Creating Your First Table
SQL Tutorial for Beginners 2: Creating Your First Table
Corey Schafer
54 SQL Tutorial for Beginners 3: INSERT - Adding Records to Your Database
SQL Tutorial for Beginners 3: INSERT - Adding Records to Your Database
Corey Schafer
55 Linux/Mac Terminal Tutorial: Navigating your Filesystem
Linux/Mac Terminal Tutorial: Navigating your Filesystem
Corey Schafer
56 Python: Ex Machina Easter Egg - Hidden Message within the Code
Python: Ex Machina Easter Egg - Hidden Message within the Code
Corey Schafer
57 Mac Tip: New Split Screen Feature in El Capitan
Mac Tip: New Split Screen Feature in El Capitan
Corey Schafer
58 Setting up a Python Development Environment in Eclipse
Setting up a Python Development Environment in Eclipse
Corey Schafer
59 Git Tutorial: Fixing Common Mistakes and Undoing Bad Commits
Git Tutorial: Fixing Common Mistakes and Undoing Bad Commits
Corey Schafer
60 SQL Tutorial for Beginners 4: SELECT - Retrieving Records from Your Database
SQL Tutorial for Beginners 4: SELECT - Retrieving Records from Your Database
Corey Schafer

This video teaches the basics of slicing lists and strings in Python, including how to use positive and negative indexes, and how to extract subsets of elements from lists and strings. It provides a comprehensive overview of the syntax and practical applications of slicing in Python.

Key Takeaways
  1. Access single values in a list using positive and negative indexes
  2. Extract a range of values from a list using the start_value:end_value syntax
  3. Mix and match positive and negative indexes to access specific elements
  4. Print the list from index 1 to the end
  5. Print the list from index 5 to the end
  6. Print the list from index -1 to 8
  7. Print the list from index 2 to 8 with a step of 1
  8. Print the list from index 2 to 8 with a step of 2
💡 The key insight from this video is that list slicing in Python allows for the extraction of a subset of elements from a list using the syntax list[start:stop:step], where the start index is inclusive, the stop index is exclusive, and the step value determines the interval between elements.

Related AI Lessons

Up next
AI in Care - Katie Furey, Pairly.com
The Access Group
Watch →