5. Lists [Python 3 Programming Tutorials]

codebasics · Beginner ·🛠️ AI Tools & Apps ·7y ago

Key Takeaways

Covers lists in Python, including creation and methods

Full Transcript

today's topic is list in Python so what is list let's understand this by working on this sample problem we all go to grocery stores what do we do when we are about to leave for grocery stores yes you guessed it right we make our grocery list the list of items that we won't buy from grocery store if you want to tour this list of items in Python and bring them later on with whatever we have learnt so far what you will do is you will essentially create different variables for different items for example you say item 1 is equal to bread item 2 equal to pasta let's say these are all the items that you are buying from a grocery store and item 3 equal to let's say you want to buy some fruits so you will create these kind of variables but the problem here is you are creating so many variables if you have like certain items to buy from a grocery store you will create ten variables and that's a little bit tedious a programming language should have something better correct list comes to a rescue in this kind of situation so to do the same thing in a list what you do is again you create a variable which is going to store lists so you say items equal to now to start a list you'll open square bracket and then you add all your items in the list separated by comma faster my third item that I want to buy is fruit and I love veggies so let me buy some veggies as well and closing square bracket where I hit enter I just created a list and the list got stored in items variable so when I print items I will see this list so as you see in the diagram the way list is stored internal in computer memories is it is a sequence of memory locations where each of these items are stored and these locations are accessed by the index so items will point to a starting location and each of these items will be the same bread will be at 0th index pasta is at 1 index and so on so this is similar to strings if you have this into my strings episode you're kind of recall some similarity between that and the list all right now if you want to access each of these individual items you can do so by using the index so if you use bracket 0 hit enter then it will print bread how do you access this item ok again use the index and this is stored 0 1 2 so fruits is at number 2 cool now let's say I made a list I'm about to go to grocery store and I change my mind now instead of bread I want to buy chips how do I modify my list well just two items 0 so this is the location at which bread is store and you say equal to sign chips ok so what we just did is in this list at 0 2 location we placed chips instead of bread so now if you print items you see you notice the difference between this list and this one the first item got changed from bread to chips so this is how you change the value of individual element within a list ok now how do you access a range of elements so for example I want to print first two elements from the list again you can use the sub index range if you recall from my string episode so you just use the starting index so which is 0 so let's say you want to print in this list you want to bring chips and pasta so tip is stored at Sarat location and Colin pasta is at one but you don't specify one because of a Python index box is the second index is always excluded so you have to say 1 plus 1 which is 2 and if you do that it will print chips and pasta let's say you want to print the last element in the list for that you can use a negative index so negative 1 means index number 1 but from the end all right you can also use a pen there is a method called a pen which can be used to append an element so for example let me just reinitialize my list if you do ctrl C like this and control V it will again free initialize the list so your items is again back to the same state now you are going to grocery store with this list of items and all of a sudden you recall that you have to buy butter as well so you will just say items dot append bracket butter ok let's see what happens nice so you see like this was a original list up till here and when you say up and it just appended butter towards the end but now in the grocery store butter is in the near most aisle as the bread so you I really want to have butter at the 2nd place in your individual list how do you do that so let me again we initialize the list so that we are back to the original state so let me just confirm ok I don't have butter now I want to now insert butter at this location immediately after bread so I say items dot insert okay so what insert will do is rather than a pending at the end it will insert an element at a specific location as you see in a tooltip it is showing you some help so index means the in at which you want to insert it so at which index you want to insert it so bread is 0 and 1 so at 1 is the location where you want to insert it so butter and enter let's see what we got voila so after bread now I have butter alright now what do you do if you want to join two lists so you're going to grow sister and you prepare a list of all food items so let's say you have all your food items here and your wife made a list of all the bathroom items right so you now have got like two lists one is the food items and your wife has made a list of all the bathroom items so let's say this is the list that she's having and as you both are going to go sister together you want to just have a woman one single list so how do you do that so you can say my items is equal to 4 plus bathroom so this way by using plus sign so what you're doing here is food is a variable pointing to one list bathroom is a variable pointing to another list when you say food plus bathroom it is going to join these two lists together and on the left hand side of equal to sign is another variable called items and it will assign that list to it so if you print items you'll get this so now you've got the combined list okay now if I have a list which is food and if I want to insert just single item can I do like this let's try it what happens so I want to buy soda as well if you do this then it won't work because you cannot add string or any odd number to a list whenever you're doing plus sign here to make sure on the left hand side as well as on the right side oh the things should be list so that was about concatenation you can figure out the length of the list using this command line so now I got all these items in my list right so I have these many items and I want to know how many items I'm going to buy from the grocery store so for that use this function length so when you say le and opening bracket items it is going to print the land of these items so as you see it's 5 1 2 3 4 5 ok alright now I have this long list and I'm doing my shopping I want to know whether I added fruits to my list or not how did I do that I can just read this list one by one this is a simple one but less if you're a long list and if they are using a powerful programming language like Python you don't want to read these items one by one there should be a better way of doing it right and that way is the in operator so you can say bread in items and it will say true right if you say soda in items and it says false because it didn't find so line item so whenever you want to do look up in the list you can use this operator in alright so that was all about lists thanks for watching

Original Description

This python video answers about lists. It gives information about what is a list, how to create a list, range element, the use of list method and list function. Exercise: https://github.com/codebasics/py/blob/master/Basics/Exercise/5_lists/5_lists_exercise.md Topics that are covered in this Video: 0:00 Overview 0:58 create list variable 2:06 How list are stored in computer memory 4:01 How do you access a range of elements 5:01 use append() method 7:04 Join two list 8:24 list concatenation 9:00 len() function 9:27 "in" operator Do you want to learn technology from me? Check https://codebasics.io/ for my affordable video courses. 🔖Hashtags🔖 #pythonlist #pythonliststutorial #listappend #listsinpython #listpython #removelist #addtolist #pythonlistmethods Next Video: 6. Install PyCharm on Windows [Python 3 Programming Tutorials]: https://www.youtube.com/watch?v=Vig1IeU2RYk&list=PLeo1K3hjS3uv5U-Lmlnucd7gqF-3ehIh0&index=7 Website: https://codebasics.io/ Facebook: https://www.facebook.com/codebasicshub Twitter: https://twitter.com/codebasicshub
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from codebasics · codebasics · 0 of 60

← Previous Next →
1 Python Tutorial - 1. Install python on windows
Python Tutorial - 1. Install python on windows
codebasics
2 Python Tutorial - 2. Variables
Python Tutorial - 2. Variables
codebasics
3 Python Tutorial - 3. Numbers
Python Tutorial - 3. Numbers
codebasics
4 Python Tutorial - 4. Strings
Python Tutorial - 4. Strings
codebasics
5 Python Tutorial - 5. Lists
Python Tutorial - 5. Lists
codebasics
6 Python Tutorial - 6. Install PyCharm on Windows
Python Tutorial - 6. Install PyCharm on Windows
codebasics
7 PyCharm Tutorial - 7. Debug python code using PyCharm
PyCharm Tutorial - 7. Debug python code using PyCharm
codebasics
8 Python Tutorial -  8. If Statement
Python Tutorial - 8. If Statement
codebasics
9 Python Tutorial - 9. For loop
Python Tutorial - 9. For loop
codebasics
10 Python Tutorial -  10. Functions
Python Tutorial - 10. Functions
codebasics
11 Python Tutorial - 11. Dictionaries and Tuples
Python Tutorial - 11. Dictionaries and Tuples
codebasics
12 Python Tutorial - 12. Modules
Python Tutorial - 12. Modules
codebasics
13 Python Tutorial - 13. Reading/Writing Files
Python Tutorial - 13. Reading/Writing Files
codebasics
14 How to install Julia on Windows
How to install Julia on Windows
codebasics
15 Python Tutorial - 14. Working With JSON
Python Tutorial - 14. Working With JSON
codebasics
16 Julia Tutorial - 1. Variables
Julia Tutorial - 1. Variables
codebasics
17 Julia Tutorial - 2. Numbers
Julia Tutorial - 2. Numbers
codebasics
18 Python Tutorial - 15. if __name__ == "__main__"
Python Tutorial - 15. if __name__ == "__main__"
codebasics
19 Julia Tutorial - Why Should I Learn Julia Programming Language
Julia Tutorial - Why Should I Learn Julia Programming Language
codebasics
20 Python Tutorial  - 16. Exception Handling
Python Tutorial - 16. Exception Handling
codebasics
21 Julia Tutorial - 3. Complex and Rational Numbers
Julia Tutorial - 3. Complex and Rational Numbers
codebasics
22 Julia Tutorial - 4. Strings
Julia Tutorial - 4. Strings
codebasics
23 Python Tutorial -  17. Class and Objects
Python Tutorial - 17. Class and Objects
codebasics
24 Julia Tutorial - 5. Functions
Julia Tutorial - 5. Functions
codebasics
25 Julia Tutorial - 6. If Statement and Ternary Operator
Julia Tutorial - 6. If Statement and Ternary Operator
codebasics
26 Julia Tutorial - 7. For While Loop
Julia Tutorial - 7. For While Loop
codebasics
27 Python Tutorial  - 18. Inheritance
Python Tutorial - 18. Inheritance
codebasics
28 Julia Tutorial - 8. begin and (;) Compound Expressions
Julia Tutorial - 8. begin and (;) Compound Expressions
codebasics
29 Python Tutorial - 12.1 - Install Python Module (using pip)
Python Tutorial - 12.1 - Install Python Module (using pip)
codebasics
30 Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
Julia Tutorial - 9. Tasks (a.k.a. Generators or Coroutines)
codebasics
31 Julia Tutorial - 10. Exception Handling
Julia Tutorial - 10. Exception Handling
codebasics
32 Python Tutorial  - 19. Multiple Inheritance
Python Tutorial - 19. Multiple Inheritance
codebasics
33 Python Tutorial - 20. Raise Exception And Finally
Python Tutorial - 20. Raise Exception And Finally
codebasics
34 Python Tutorial - 21. Iterators
Python Tutorial - 21. Iterators
codebasics
35 Python Tutorial - 22. Generators
Python Tutorial - 22. Generators
codebasics
36 Python Tutorial - 23. List Set Dict Comprehensions
Python Tutorial - 23. List Set Dict Comprehensions
codebasics
37 Python Tutorial - 24. Sets and Frozen Sets
Python Tutorial - 24. Sets and Frozen Sets
codebasics
38 Python Tutorial - 25. Command line argument processing using argparse
Python Tutorial - 25. Command line argument processing using argparse
codebasics
39 Debugging Tips - What is bug and debugging?
Debugging Tips - What is bug and debugging?
codebasics
40 Debugging Tips - Conditional Breakpoint
Debugging Tips - Conditional Breakpoint
codebasics
41 Debugging Tips - Watches and Call Stack
Debugging Tips - Watches and Call Stack
codebasics
42 Python Tutorial - 26. Multithreading - Introduction
Python Tutorial - 26. Multithreading - Introduction
codebasics
43 Git Tutorial 3:  How To Install Git
Git Tutorial 3: How To Install Git
codebasics
44 Git Tutorial 1: What is git / What is version control system?
Git Tutorial 1: What is git / What is version control system?
codebasics
45 Git Tutorial 2 : What is Github? | github tutorial
Git Tutorial 2 : What is Github? | github tutorial
codebasics
46 Git Tutorial 4: Basic Commands: add, commit, push
Git Tutorial 4: Basic Commands: add, commit, push
codebasics
47 Git Tutorial 5: Undoing/Reverting/Resetting code changes
Git Tutorial 5: Undoing/Reverting/Resetting code changes
codebasics
48 Git Tutorial 6: Branches (Create, Merge, Delete a branch)
Git Tutorial 6: Branches (Create, Merge, Delete a branch)
codebasics
49 Git Github Tutorial 10: What is Pull Request?
Git Github Tutorial 10: What is Pull Request?
codebasics
50 Git Tutorial 7: What is HEAD?
Git Tutorial 7: What is HEAD?
codebasics
51 Git Tutorial 9: Diff and Merge using meld
Git Tutorial 9: Diff and Merge using meld
codebasics
52 Difference between Multiprocessing and Multithreading
Difference between Multiprocessing and Multithreading
codebasics
53 Python Tutorial - 27. Multiprocessing Introduction
Python Tutorial - 27. Multiprocessing Introduction
codebasics
54 Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
Python Tutorial - 28. Sharing Data Between Processes Using Array and Value
codebasics
55 Git Tutorial 8 - .gitignore file
Git Tutorial 8 - .gitignore file
codebasics
56 Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
Python Tutorial - 29. Sharing Data Between Processes Using Multiprocessing Queue
codebasics
57 Python Tutorial - 30. Multiprocessing Lock
Python Tutorial - 30. Multiprocessing Lock
codebasics
58 Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
Python Tutorial - 31. Multiprocessing Pool (Map Reduce)
codebasics
59 What is code?
What is code?
codebasics
60 Python unit testing - pytest introduction
Python unit testing - pytest introduction
codebasics

Related AI Lessons

How to Create a Second Version of Yourself Inside Obsidian Using AI (Step-by-Step Guide)
Learn to create a second version of yourself inside Obsidian using AI with a step-by-step guide
Medium · ChatGPT
How to prepare for Spain civil service TIC exam using AI in 2026
Learn how to prepare for the Spain civil service TIC exam using AI in 2026, boosting your chances of success with technology-driven study techniques
Dev.to · David García
Going Viral! How I Created AI Kissing Videos Step by Step Easily Using AIAI.com
Create viral AI kissing videos using AIAI.com in a step-by-step process, leveraging AI technology for creative content creation
Medium · AI
How to prepare TIC teacher exams in Spain with AI (oposiciones 2026)
Prepare for TIC teacher exams in Spain using AI with these actionable steps
Dev.to AI

Chapters (9)

Overview
0:58 create list variable
2:06 How list are stored in computer memory
4:01 How do you access a range of elements
5:01 use append() method
7:04 Join two list
8:24 list concatenation
9:00 len() function
9:27 "in" operator
Up next
Low-Tech, High-Impact: Replacing Your Receptionist With a $15 AI Phone System
Maximum Lawyer
Watch →