Python Beginner Tutorial #6 - Sequences and Collections
Key Takeaways
This video tutorial covers the basics of sequences and collections in Python, including lists, tuples, and dictionaries, and demonstrates various operations and methods for working with these data structures.
Full Transcript
what is going on guys welcome to the episode number six of the Python tutorial series for beginners today's video will be about sequences and collections now in the last video we mentioned that we can use for loops to iterate over sequences but we didn't really talk about what a sequence actually is now in this video we'll get an overview over the different types of sequences and collections in Python and what we can do with them so first of all what is a sequence a sequence is basically just a collection of elements so instead of having one value assigned to a variable we can now assign a whole collection of values to variables so one structure one object that contains multiple values but can be treated as one thing now before we talk too much about the theory here let us get into the code so the first sequence type that we're going to talk about today is the so called list and to define a list in Python we use so-called square brackets so I can say my list equals and I use square brackets to define it what I'm doing right now here or what I'm creating right now here is a list and in between these square brackets here I put all of my values separated by commas so let's say 10 20 and 30 this would now be a list of the length 3 and we'll have 3 integers as elements and what I can do here is I can print the whole list as a whole object print my list and when I run it you see 10 20 30 now this object contains these three values notice that pythons typing here is again dynamic so we can mix various data types in most other programming languages we would have to define the data type of the list so in Java for example I would have to say int array or int list whatever my list to tell the compiler in Java that this right here is an integer list so it would not be allowed for me to enter a string here because it's not dynamically typed and Python this is no problem I can just define my list and I can put all kinds of values in here so true of eight point nine seven whatever I can add multiple values here and it will still work out well or fine because it's dynamically typed now one thing to know about sequences is that most of them are indexed and an index is basically just a number that gives us information about the position of an element in our collection so in programming we started counting from 0 so the first element here would have the index 0 the second one would have the index 1 and so on so we use indices to access individual elements so maybe I'm not interested in the whole list I'm interested in this string here and to get this string I just have to specify the index of the element I'm interested in so I have my list as a whole object and I can use my list to access individual values off that list so I specify square brackets here and in between these I put the index of the element in this case it would be 3 because its position 4 we always subtract 1 and we get the index so my list 3 would be the string now of course you cannot just specify any number because if you go out of the range here if you in this case we have 6 elements so the highest index will be 5 and if we for example try to access index 9 this would give us an error index error as you can see because the list index is out of range now another thing that we can do here is we can access slices of this list so maybe we're not interested in the whole list we're not interested in one value but we're interested in let's say the first 3 values 10 20 and 30 and what I can do here is I can specify the slice that I'm interested in so in this case it would be from the beginning and then I use a colon sign to say from the beginning up until and in this case I enter a 3 to tell the interpreter up until index 3 which means that index 3 is not included so when I have something on the right side of the comma it's not included so in this case it would be index 0 1 and 2 but not index 3 so I would get the first three numbers however and this might be a little bit confusing when I start when I put something out on the left here it is included so I can specify a range 1 up until 3 which would use index 1 which would in pollute index 1 but not index 3 so I would get 20 and 30 here as you can see so just remember the element at the index of the right side here is not included but on the left side it is included and another thing I can do is I can access elements from right to left so maybe I don't want to say index I don't know 5 4 sorry but I want to say the 2nd element from right to left and this might be also confusing because when we say we want an index or access from right to left we use a minus but we then start counting from 1 so if I want to access the true here I have to specify minus 2 whereas when I access it from the left I start counting from 0 so 0 1 2 3 4 but here it's 1 & 2 so this case minus 2 would give me the second element from right to left now we can use the same notation to manipulate or change values of a sequence so we can not only access them for reading but I can also say my list I don't know let's change the string my list 3 would be now another string we would just replace the value of this index or add this index so print my list run it and you can see that I replaced the string of course it does not only work with the same data type so I don't have to replace this string by a string I can also replace a string by a number let's say 6 point 5 4 and instead of a string I would now have a number here so it's totally fine we can just replace all the values what we cannot do is just a pen devalue so I cannot say index 6 that does not exist is something because for this we will get to know a method but we cannot just access a an index that is not assigned yet or it's not there yet now last but not least before we get into list operations functions and methods let's quickly take a look at how to iterate over lists with for loops because this is something that we talked about in the last video now it's quite simple actually all we have to do is we have to say for some control variable X in my list and then I just print X for example in this case it would print all of the values one after the other because with every iteration X the value of X becomes the next element in the list so I start with X being 10 in the first iteration 20 in the next iteration and so on and we can just print all the values or we can process them so if we have a list of numbers 1 2 3 4 5 I can for example just print 2 to the power of X which would give me all the powers of 2 up until 5 and I can use the values of the list for or as an input as you can see now let us talk about what we can do with these lists first of all we can perform some basic operations on this for example we can just add two lists to append one list onto the end of another list so list X 1 2 3 and Y is 4 5 6 and when I add these two lists I don't get the result off 1 + 4 2 + 5 3 + 6 or something like that I just add one list at the end of another list so x + y would give me just a new list which has all the numbers in it of course this works with all kinds of data types so have to be same data-type we just add the two lists we just merge them if you want and that not another operation that we can do here is we can multiply lists by scalars so if I multiply X for example by four what I'm doing here is I'm not multiplying 1 by 4 2 by 4 3 by 4 I just repeats the list four times so I get a new list with the same values four times in a row besides that we cannot use any operations really because we cannot subtract lists we cannot add numbers to them we cannot divide them we cannot remove we cannot multiply a list by another list so these are basically the operations that we can do we can add two lists or multiply a list by a scalar but what we do have is a lot of different functions and methods for lists so let us first have a look at the three basic functions of list so just list functions that give us some informations about a list and these three functions would be Len Max and min so the Len function gives us the length of a list in this case how many elements list contains so in this case will be free so Len X would be three we're going to print that in a second print so the length function basically just tells us how long the list is how many elements it contains the max value in the or the max function in the min function always returned the smallest value and the biggest value of a list so max of X would be 3 and min of X would be 1 of course and of course we have to print it as well prin there you go oh I forgot a parenthesis here so as you can see three three and one so length of three max value three and the minimum value of one however take care here because if I have a string in here hello or a boolean we have a problem because I can still get the length of this list in this case would be five but I cannot get the maximum value and the minimum value because you'll see that I cannot just compare with this comparison operator that we already know I cannot check if a string is greater than an integer with some datatypes it works I can check floats and integers but I cannot really check if a string is bigger or larger or less than an integer so we would get this error here it's type error so these are the three basic list functions now let us get into some list methods and there are a lot of interesting methods but it would be too much for one video to cover all of them so in this video we're going to focus on the most important ones especially for now probably the most important list method is the so called append method what it does is it just appends one value at the end of list because we said we cannot just say X off in this case the biggest is I cannot just say X of five equals something because the index does not exist but if I want to add a value to a list I just say X dot append and some value can be six or new value as a string and when I then print the list you'll see that there's a new value in it so this is the basic append method just adding one more value to the list now if we don't want to add this value at the end of a list but we want to add it as a specific we want to insert it at a specific index we can use the so-called insert methods so X dot insert and then I have to specify the value and the index for example - oh sorry the other way around first the index and value so add index to I want to access I want to put the value new value and as you can see it now shifts everything to the right so it goes to index two and shifts the three values to the right and inserts the value new value at the index two now we don't want to add an element but we want to remove one we can use the remove method the remove method just accepts a value so X don't remove and not an index so I don't remove an element as a specific index I say remove the value of hello for example and it would just remove the value hello if it's contained in the list if not for example ABC is not contained in the list it's not contained in the list then of course we get an error because X is not in list value error we cannot remove something that's not there however if you want to remove something at a particular index you can use the pop function because the pop function allows you to remove an element at a specific index without knowing what this element is so maybe I want to remove the value at in or the element at index two in this case this would be three and when I say X pop two we no longer have the three in there now another useful method here is the so-called index method what it does is we basically pass a value to it and if this value is in our list if the list contains this value this method returns the index of the element to us so when I say X index hello it would return to me I would have to print this of course print yes so if we print the index we get three as a result because the element hello is at the index three and what I can do of course I can say X dot pop X index hello this would basically just pop the element at the index from off the value hello so I would get first the index off the value I'm looking for and then I can pop this element so it would basically just remove hello this is just a remove method if you want but it works as you can see and last but not least let's talk about the sort method and this method basically just sort our list so if we have a list of one two three four okay this already sort it let's say fifty five six one ninety nine two three what I can now do is I can print X or no first I say X dot sort and then I can print X and as a result you'll get a sorted list so one two three six $55.99 and this method basically sort or list now there's an alternative to that we can also use the sorted keywords so I can say print sort it X this is a Python function which just returns the sort of list so you can either say ax out sort or you can just return the sorted version of X the difference here is that if you say X dot sort it really changes the list so it it's now a sorted list X becomes this list and if you just print the sorted list or the sorted version of X you just print it but X still remains the list it was in the beginning now let us get to the next type of sequence the tuple tuples are exactly like lists except for the difference that they are immutable so we cannot change the values or the structure of tuples and also they are defined with parentheses and not with square brackets so I have the list 1 2 & 3 or I have to pull 1 2 & 3 now as I said they're exactly the same except for the difference that we cannot change a tuple so we can still get the length of a tuple we can still access the individual elements of a tuple by it for example just saying print y2 this would give us 3 because we can do the same thing but what we cannot do is we cannot change values I cannot go ahead and say Y 2 equals 10 for example this is not possible because a tuple is immutable and as you can see type error tuple object does not support item assignment because a tuple stays the same we can of course use the same methods and functions that we use for lists except for all of those who manipulate something so we're not able to sort the tuple for example because the tool has to remain the same we cannot just change the order it stays exactly the same however if we have a tuple for some example some functions return tuples some inputs or tuples some data which we will load as a tuple if we want to change it we can just type cast it so let's say X is the tuple here what I can then do is I can say x equals list of X and now of course I could also change the values even though it's a tuple and I will then have a list of course I can then typecast it back into tuple so after changing the value I can say x equals tuple X again and I would have again an immutable tuple with a changed value the last sequence type that we're going to talk about today is the dictionary now dictionaries work a little bit differently than the other sequences because they're composed of so-called key value pairs and they're not indexed now a key value pair consists of an unique key which is the replacement for the index and a value that belongs to that key so the access of value of a dictionary we don't refer to the index but to its key for example we could define the dictionary person and this dictionary would have different keys and different values now a dictionary is always defined by curly brackets or not square brackets or parentheses but curly brackets and we can start by specifying the first key here and a person could have a name so the string name would be the key that we access to get its value and the value to this key the value that belongs to this key comes after the colon here in this case we could say this is Mark and then we separate again this key value pair here so this right here is a key value pair and I separated by a comma from the next key value pair and the next key value pair could be H and 25 for example and the next one could be gender and mail whatever so this would be a dictionary with three key value pairs so I could print the person dictionary here and this would be the whole dictionary now if I want to access some value here I don't say give me person zero or person one doesn't work but I say give me person H so I use the key to get the value this is why the key has to be unique because I can have multiple keys different keys with the same value but I cannot have the same keys because if I have 3 times name when I access name what result will I get I have to have a unique key so when I access H I get 25 when I access name I get mark and of course the key doesn't always have to be a string at least not in Python I can also have true as a key it's quite stupid but I can have true as a key and eighty-eight is a value for example so when I say print person true I would get 88 it's not very reasonable to use a boolean as a key but you can do it now to add a new key or a new key value pair to this dictionary what we do is we say person new key equals some value I don't know 67 and when we do that so we don't have an append method here we just specify a new key and it works so print person would now have another key value pair at the end new key 67 so now let's talk about three very important dictionary methods because they're quite useful to us and they're quite similar and these three methods are items keys and values and you might already guess what they're going to do for example when we say print person items with this does is it returns a list of all items of all key value pairs when I say person dot key or keys sorry it returns a list of all the keys so name age gender true new key and so on and when I have person that values I get all the respective values so these are very important dictionary methods that we can use so actually it's not a list I I said something wrong here it's actually if you look at it it's a tuple of a list of tuples so we have a tuple dict items which contains a list and this list contains the tuples of the key value pairs and if we don't have pairs we just have a tuple of a list with elements so if you're interested in more list functions list methods just check out the Python documentation because as I already said I cannot go into all the functions here and how they work because this would be way too long for a video but if you're interested in em check out the link in the description you'll find a link to the documentation of dictionaries lists tuples and so on and there you can read a lot about the different functions and methods that you can use now the last point for today's video are the so called membership operators and also an operator type that we also skipped in the third part namely the identity operator so membership operators the to membership operators are in and not in and they're quite simple to understand because we've already used them in for loops and what they basically do is they check if an element is contained in a sequence so they return true or false so a boolean and when I say print or first of all that lets define a list x equals one two three and now I can print a two in X and I can print seven in X and of course the first one will return true and the second one false because seven is not contained in X and two is contained in X and of course I can use the same with not interesting gates as notice actually just a logical operators are not in with negate in which would just flip the results operators that we need to talk about are is and is not actually they're not very related to the topics of sequences but I thought since we skipped them in the operators video right now it might be a good place to talk about them and basically they're quite simple they just check if something is a type for example so I can have a value X equals 10 and now I can check if type X is int I can print X is int and otherwise I can print X is not int so in this case it will print X is int and if I make X to be a string X is not int so the is or is not we can also use it's not like this these identity operators just allow us to check if something is the type of something so that's everything to know about sequences and collections for now in the next video we're going to talk about functions and parameters there's a lot more to learn here so stay tuned and keep watching these videos if you liked the video please hit the like button and also if you want to see more subscribe to this channel and feel free to ask questions and give feedback in the comment section down below thank you very much for watching and see in the next video bye [Music] you
Original Description
In today's episode we are learning about the different types of sequences and collections in Python!
Python Sequences Documentation: https://docs.python.org/3/tutorial/datastructures.html
Website: https://www.neuralnine.com/
Instagram: https://www.instagram.com/neuralnine
Twitter: https://twitter.com/neuralnine
GitHub: https://github.com/NeuralNine
Programming Books: https://www.neuralnine.com/books/
Outro Music From: https://www.bensound.com/
Subscribe and Like for more free content!
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeuralNine · NeuralNine · 7 of 60
1
2
3
4
5
6
▶
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
More on: Tool Use & Function Calling
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
X now offers an MCP server to make its platform easier for AI tools to use
TechCrunch AI
n8n Automation Repurpose Video Content: The 2025 Production Guide
Dev.to AI
You’re Still Paying $200/Month for AI Tools You Could Replace With a Free Local Setup Tonight
Medium · Data Science
Top 10 AI Tools Every College Student Should Know in 2026
Medium · AI
🎓
Tutor Explanation
DeepCamp AI