Python Lists || Python Tutorial || Learn Python Programming

Python Lists  ||  Python Tutorial  ||  Learn Python Programming


Order.. matters.. And in Python, lists make
it easy to work with ordered data. Lists are a built-in data structure for storing and
accessing objects which belong in a specific sequence. We will now learn how to create
and use lists, and we will do so in a linear and orderly fashion… There are two ways to create a list. One way is to use the list constructor. But a simpler
and more common way is to use brackets. When creating a list, you can also pre-populate
it with values. For example, let’s create a list with the first few prime numbers. You can always add values later by using the
“append” method which allows you to add new values to the end of the list. Let’s
append the next two prime numbers: 17… and 19… If you display the list, you will see it contains
the new values. Notice how lists preserve the order of the data – this is different
from sets. In sets, the order is not important. In lists, order is everything.
You do not have to view the entire list. If you want to see a specific value, you can
access it by its index. In computer science, you start counting with 0, not 1. So the elements
in our list “primes” are indexed 0.. 1.. 2.. 3.. 4.. 5.. 6.. and 7..
To view the first item, you type the name of the list, and the index in brackets. The
first item is 2. The second item has index 1, and the second
item is 3. And so on… Notice how the indices increase by one as
you go from left to right. And they decrease by one when you go from
right to left. When you get to the beginning, the index is
0. If you decrease the index once more, you get -1. Here, Python wraps back around to
the end of the list. So the last item is -1, the next to last is -2, and so on.
This is convenient when you want to look at the values at the end of a list. The last
item is 19… The next to last prime is 17… And we reach the beginning of the list with
index -8. Be careful – you can only wrap around once.
If you try to find the value of index -9, you get an index error. Another way to access values in a list is by slicing. This lets you retrieve a range
of values from your list. We will continue to use our list of primes. To slice this list, type the name of the list,
bracket, a starting index, a colon, a stopping index, then a closing bracket… The result
is a sublist that starts at index 2, and continues until it reaches index 5. Be careful. Slicing
includes the value at the starting index, but excludes the stopping index. The beginning
value is included; the ending value is not. One more slice… This will start at the beginning,
which is index 0, and continue to index 6, which is 17. It will not include the final
number, so this slice includes the primes from 2 through 13. Lists can contain more than prime numbers. They can contain integers…booleans… strings…
floats… and even other lists. Many languages require lists to contain values
of the same type, but not Python. With Python, you are free to insert multiple data types
in the same list. Lists can also contain duplicate values. Here
is another way lists are different from sets. For example, suppose you want to record the
numbers you roll on a pair of dice. Pretend you roll a 4.. 7.. 2.. 7.. 12.. 4 and 7..
If you look at the list, all the values are there, even the repeated rolls. You can also combine lists. To see how, create two separate lists:
A list of numbers… And a list of letters…
To combine these two lists into a single list, use the plus sign.
Numbers + letters equals 1, 2, 3 .. a, b, c… But order matters. If you reverse this and compute letters + numbers
you get a, b, c.. 1, 2, 3.. Combining lists is called “concatenation”. Observe. The list of numbers.. and the list of letters are unchanged. There are many other methods for working with lists. To see them all, pass any list to the
directory function. To learn how to use one of these methods,
use the help function. For example, there is a method for reversing a list. The help
text gives full details on what it does and how to use it. Lists start at 0 and end… … they end precisely when you are finished. You can slice them…
You can concatenate them… You can reverse them… You can even clear them… If I were
to make a list of all the uses of lists, I would have a very, VERY long list…

88 thoughts on “Python Lists || Python Tutorial || Learn Python Programming”

  1. I didn't understand why Python had so many data-collection data structures, until I learned other languages that didn't have nearly enough. Using an older version of JS right now on Khan Academy and I REALLY REALLY miss some dictionaries. Even having sets AND lists is suddenly a nostalgic luxury.

  2. I like your tutorials very much. I love that all of them aren't boring, narrated with a boring male voice which stays in front of a computer without care for life. Thumbs up and subscribe!

  3. Dam.. Why wont this work!!!
    a=[1,234,45,76,98,3,2,2,1,145,446,56,76543,76,576,76,86,58,685,8765]
    for line in a:
    U=len(a)
    B=input("Please enter a number")
    if B in range (a[0:21]):
    print(A[0],a[8],a[5])
    else:
    print(" not vaild for this list")
    keep getting
    TypeError: 'list' object cannot be interpreted as an integer

  4. What about adding to a list? Is there some add method?

    ps: the dir showed an _add_ method (with this underlines), but it doesn't seem to work

  5. Very Well done!
    That sound you hear is me clapping!!
    Could you please show how to use Lists to study Linear Algebra.
    No. Numpy is not allowed. I don't want Numpy to do it for me. I want to know how to do it.

  6. I wish I was famous so I can share all your videos. 🙁 I don't have any other social media accounts and this channel deserves more subscribers.

  7. Your video was really good. Great explanations. I especially like the fact that you were very clear and detailed with defining #concatenation and one can identify an element that is connected to a particular index. It was really helpful to me that you did not stray off topic by talking about your cat or what you had for lunch or the position of the camera. Sometimes when Youtubers do that during an instructional video, it is annoying and distracting. Thanks for the great video! 👍

  8. This is my favorite resource for learning Python lately. Short and to the point explanations. I don't use Python enough. In fact, I only seem to use it in the context of working with the Amazon AWS CLI tool.

    Nevertheless, I got a website in PHP that computes does some computations at night. My buddy turned me onto Python when he wrote a sample script that did the job of a PHP script in like a few seconds whereas it took the PHP script like almost 5 minutes to complete. I'm gonna figure out how to port those scripts into Python in my leisure time at night and it's gonna be fun as hell yo! It'll take me some time to figure out but I think it'll pay off later when all those cron jobs are complete faster.

    Step 1. Figure out how to work with Lists.

    GIT-ER-DUN!!!!

  9. Alexander Lysberg

    I like to think of slicing as the first unit starting from 0, and the second unit starting from 1. If you have a list of [a. b. c. d. e] and you want the 2:4 slice, you can count a = 0, b = 1 c = 2 (start slice). Then you can count again a = 1, b = 2, c = 3, d = 4 (end slice).

    Because "2 to 4 but not including 4" is such a hassle to say in the head.

  10. Plot twist: this is an AI machine teaching us this.. the coded AI bot is teaching us… to code ? 🤔😂

  11. I am using Windows10 and I have write some codes to scrape information from a website but I can't copy anything from my command prompt or anaconda prompt. I figure it out that my mouse dosen't working on both command prompt. Can anybody help me?

  12. Great video! I love the whole futuristic look. just sub, hoping to learn python to try to eventually head to C++.

  13. reverse(…) method of builtins.list instance
    L.reverse() — reverse IN PLACE
    Okay how the hell does that tell me anything much lesss everything? I type
    reverse(mylistname)….I get an error
    I type L.reverse(mylistname)….I get an error.
    ???????????????????????????? really tells me sooo much. Reverse will REverse *inplace*? Wouldn't a clearer method be "will invert entries" or "will change order making last entry first, first entry last etc"??

  14. freakin awsome video i feel like i'm in some game and after a video when i enter my text editor like i'm in freakin army mission lol.

  15. hi, I need an example of inputting a list(i have a numbers list) with a square parenthesis and a comma from the user. the sooner the better. thank you so much, your clips help so much, I can't thank you enough. ahttps://www.youtube.com/user/SocraticaStudios

  16. when I use:
    dir(numbers)
    I get some results and the first 3 are:
    '__add__', '__class__', '__contains__'
    But how to use these classes which contains double underscores?

  17. Esteban Escudero

    I really enjoy your videos. You make learning very easy and interesting. Thumbs up for the length of each video also. They are short as they should, but with plenty of useful info. You have won a subscriber. 🙂

  18. This woman looks and sounds exactly like Wendy Rhoades on Showtime's Billions, which is one of the best shows after Breaking Bad

  19. To get the list reversed, but not reverse the list in place: example[::-1]
    That way you get the reverse without changing the list order. I figured that one out via typo.

  20. Imagine these lists:
    [1, 3, 4, 5, 6, 7]
    [4, 5, 4, 6, 8, 9, 11, 34]
    How do you get a list from second item on those lists to the end but not the last 2 items from each list?

Leave a Reply

Your email address will not be published. Required fields are marked *