Python Tutorial for Beginners 4: Lists, Tuples, and Sets

Share
Embed
  • Published on May 17, 2017
  • In this Python Beginner Tutorial, we will begin learning about Lists, Tuples, and Sets in Python. Lists and Tuples allow us to work with sequential data, and Sets allow us to work with unordered unique values. We will go over most of the methods, learn when to use which data type, and also the performance benefits of each type as well. Let's get started.
    The code from this video can be found at:
    github.com/CoreyMSchafer/code_snippets/tree/master/Python-Lists
    Watch the full Python Beginner Series here:
    thexvid.com/p/PL-osiE80TeTskrapNbzXhwoFUiLCjGgY7
    Slicing Video: thexvid.com/video/ajrtAuDg3yw/video.html

    ✅ Support My Channel Through Patreon:
    www.patreon.com/coreyms
    ✅ Become a Channel Member:
    thexvid.com/channel/UCCezIgC97PvUuR4_gbFUs5gjoin
    ✅ One-Time Contribution Through PayPal:
    goo.gl/649HFY
    ✅ Cryptocurrency Donations:
    Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
    Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
    Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot
    ✅ Corey's Public Amazon Wishlist
    a.co/inIyro1
    ✅ Equipment I Use and Books I Recommend:
    www.amazon.com/shop/coreyschafer
    ▶️ You Can Find Me On:
    My Website - coreyms.com/
    My Second Channel - thexvid.com/user/coreymschafer
    Facebook - facebook.com/CoreyMSchafer
    Twitter - twitter.com/CoreyMSchafer
    Instagram - instagram.com/coreymschafer/
    #Python

Comments • 423

  • Lelre Ferreira da Silva

    How can I receive informations from the keyboard and save it in a list using for-loop?

  • Esraa Mohamed
    Esraa Mohamed 6 days ago +1

    Thank you for your hard work

  • Akagra Srivastav
    Akagra Srivastav 6 days ago

    Hello Corey
    I have a question regarding empty set.
    When I try to create an empty set using
    empty_set = {}
    If I print empty set, I get {} in the console.
    If I create empty set using
    empty_set = set()
    If I print empty_set here, I get set() in console
    WHY ?

    • Corey Schafer
      Corey Schafer  6 days ago

      empty_set = {} doesn't create a set. It creates an empty dictionary. In order to create an empty set you should use empty_set = set()

  • Micheal Jones
    Micheal Jones 11 days ago +1

    26:06 art students need math, too ;)

  • Reneeke9
    Reneeke9 13 days ago

    HEy Corey! I am trying to remove the duplicates with a function, check out my code.
    def Dduplicates(x): # as in delete the duplicates
    x = set(x)
    x = (x)
    print(x)
    list3 = [4,4,4,4,4,4,5,5,5,5,5,6,6,6,6]
    Dduplicates(list3)

    print(list3)
    now when i use my function, it will give me {4, 5 ,6}
    but when i print list3, it will give me all the double nrs again.
    how can i replace the Original list3, with the list3 that is generated from within the function?

  • s baskaravishnu
    s baskaravishnu 16 days ago +1

    your videos are excellent. Your videos are very much useful to me, Many thanks for that. My warm regards to you.

  • Shashwat Tyagi
    Shashwat Tyagi 20 days ago

    16:40

  • Noam Tamarin
    Noam Tamarin 21 day ago

    could someone tell me how to make the colour of my methods / functions look like Corey's?

    I installed all packages as instructed in the first video, but for example in this video, my 'print', 'append, and '=' aren't pink as his are, they're white

  • Mr Perfect
    Mr Perfect 21 day ago +1

    thanks for such useful lessons

  • Mandeep Vayeda
    Mandeep Vayeda 24 days ago

    why do we create empty lists, tuples, sets?

    • Corey Schafer
      Corey Schafer  23 days ago +1

      It depends on what you're using it for. Sometimes it's used to represent no results.

  • Rohan Sirohia
    Rohan Sirohia 25 days ago

    How print only education of the sub list like at 8:02 where you have showed printing the list stored inside another list

  • Sahil Khotkar
    Sahil Khotkar 27 days ago

    which IDE is this?

    • Corey Schafer
      Corey Schafer  23 days ago

      Hey There. I use Sublime Text for my tutorials since it's pretty minimalist. I also use VSCode for my day-to-day coding. I made a video on how I set up my Python development environment in both of those editors. You can find those videos here:
      Sublime Text - thexvid.com/video/xFciV6Ew5r4/video.html
      VSCode - thexvid.com/video/06I63_p-2A4/video.html
      Thanks!

    • M.S. Sudarsanan
      M.S. Sudarsanan 26 days ago

      py charm

  • Yosi Gilad
    Yosi Gilad Month ago +1

    Thank you!

  • R M
    R M Month ago

    Would it be possible to change list items like this?:
    fruits = ['apple', 'orange', 'banana']
    fruits[0] = 'pineapple'
    print(fruits)
    /////////////////////////////////////////////
    ['pineapple', 'orange', 'banana']

  • Paterson Djemi
    Paterson Djemi Month ago +1

    you are an amazin teacher ! stay blesses. Thanks for sharing all that with us

  • surekha c
    surekha c Month ago

    Hi Corey... How to sort alpha numeric list?
    Example:list1=['copy::settings_1.png', 'copy::settings_2.png', 'copy::settings_10.png', 'copy::settings_11.png', 'copy::settings_3.png', 'copy::settings_4.png']

  • Yasin Abdi
    Yasin Abdi Month ago

    5:00 physics is actually the third index in the list but you said the second

    • Yasin Abdi
      Yasin Abdi Month ago

      @Corey Schafer but you said our second index which is physics while its the third i know its in [2] . but logically if you call physics the second index then what should you call history

    • Corey Schafer
      Corey Schafer  Month ago

      Yasin Abdi Yep, that will have an index of 2 even though it is the 3rd item

    • Yasin Abdi
      Yasin Abdi Month ago

      @Corey Schafer im taking about the item physics index

    • Corey Schafer
      Corey Schafer  Month ago

      Indexes start at 0. So the third item of them list has an index of 2

  • Shiva
    Shiva Month ago

    List : 0:00
    Tuple : 20:05
    LIst : 23:34

  • westfield90
    westfield90 Month ago

    Can you sort a Tuple? If so isn't that mutating the tuple?

    • Corey Schafer
      Corey Schafer  Month ago

      You can, but it creates a new object instead of modifying the existing object.

  • Time Passer
    Time Passer 2 months ago

    Why some tuples with one item only ends with coma before the closing parenthesis?

  • Himanshu bhandari
    Himanshu bhandari 2 months ago +2

    how you did.....select lines and insert # and remove # in front of all lines just pressing a single key(which key??🤔🤔🤔)

  • Steve Glover
    Steve Glover 2 months ago +4

    Wow, 2 years old and still the best beginner python tutorial I could find. You obviously enjoy what you do, and it shows. Thanks for all your hard work.

  • Bishoy Zakaria
    Bishoy Zakaria 2 months ago

    There is a lot of info here, which is great but how do we use these tools to actually code. basically, how would we apply them?

  • arpita behura
    arpita behura 2 months ago +1

    I must say excellent stuff packed in a video. Awesome Corey!!!

  • Michelle Mouton
    Michelle Mouton 2 months ago

    What are you pressing to comment out and uncomment so many lines at once 🤩

    • Corey Schafer
      Corey Schafer  2 months ago

      That’s a keyboard shortcut in Sublime. “Cmd + /“ for Mac and “Carl + /“ for Windows. I made a video that covers my favorite features and shortcuts in Sublime. You can find that video for your operating system here:
      Mac: thexvid.com/video/TB3qztdM7V8/video.html
      Windows: thexvid.com/video/9FPFogHkgOc/video.html

  • rancidsox888
    rancidsox888 2 months ago

    Do you have any relevant practice exercises (or reccomendations) that follow the pace/content of your videos? I'm learning so much and would love to have exercises to practice!

  • mohamed subhi bouchi
    mohamed subhi bouchi 2 months ago

    can I convert types ? for example converting set into list or converting tuple into set

  • Arun Thayaleveedu
    Arun Thayaleveedu 3 months ago +1

    Thanks Corey …….

  • Mahfuz Raihan
    Mahfuz Raihan 3 months ago +1

    Thank you so much for this. everything I clear about list exactly what I wanted.

  • BuildoThon
    BuildoThon 3 months ago +1

    best video

  • Issac Wiggins
    Issac Wiggins 3 months ago

    Hi Corey, first I would like to say awesome videos loving them. However, I do have question since I am a noob scripter. In Lists[] you use extend to combine 2 different lists together, for sets, you use union do those methods work specifically for those functions?

  • Bohemia
    Bohemia 3 months ago +1

    Best described I have come across.

  • Chandra V
    Chandra V 3 months ago

    Is it possible to make video series on Docker & Kubernetes!

  • Syed Abdul Rahman S
    Syed Abdul Rahman S 3 months ago +1

    Hi All,
    seq = [1,2,3,4]
    course.insert(-1,[5,6,7]) , does not add at the end of the list?
    print(seq) ---> [1, 2, 3, [5, 6, 7], 4]
    why it so?

    • Adarsh Gupta
      Adarsh Gupta 3 months ago

      Good question friend... Remember if we insert it actually inserts the list before the index which is passed.
      For putting it in the end you need to use append function.
      The reason is that these things work as linked lists...(a data structure):)

  • disterasd123
    disterasd123 3 months ago +1

    at 19:53 i think he meant space hyphen space. Other than that a really good video!!!

  • Ss Ojha
    Ss Ojha 3 months ago

    Does the 'union' method works with lists as well?

    • disterasd123
      disterasd123 3 months ago

      I guess its not possible, I tried:
      >>> list_1 = ['a', 'b']
      >>> list_2 = ['a', 'c']
      >>> print(list_1.union(list_2))
      Traceback (most recent call last):
      File "", line 1, in
      print(list_1.union(list_2))
      AttributeError: 'list' object has no attribute 'union'

      But i guess if you had the potential list of intersecting items, its possible to do print( 'what you wanna find' in list _1) then do it for list_2 to check.

  • Lei Gi Ya
    Lei Gi Ya 3 months ago

    Hi, I used print(popped) after popping off 2 values but popped continues to show only the first value which was popped off. Help?

  • Saganga Kapaya
    Saganga Kapaya 3 months ago +2

    Powerful thanks.

  • Thomas Eulogy
    Thomas Eulogy 3 months ago

    nums.sort(reverse=-1) and nums.sort(reverse=True) works too. Whats the use of -1 and True? When it is handy to use 1st one and when the 2nd one? In hope this q isn ´t to dumb. n00b

  • Tin tin
    Tin tin 3 months ago +1

    Nice

  • Carsten Frers
    Carsten Frers 3 months ago

    Thank you very much, Corey

  • Desean Lothian
    Desean Lothian 3 months ago +1

    Ty for your support

  • Treaty Loops
    Treaty Loops 3 months ago

    Can list or tuple perform intersection or difference or union?

  • The Musical Feelings
    The Musical Feelings 3 months ago +1

    Thank you so much for the video sir. It really helped me. It took me 3 days to cover all :) :)
    Do you think it is normal for someone to take 3 days for lists, tupils and sets? Please let me know :) :)

  • vikrant kamble
    vikrant kamble 3 months ago

    Too big a video to follow.

  • SHRIKANTH Y
    SHRIKANTH Y 3 months ago +1

    Thank You and explained pretty well..

  • Ryan Bahillo
    Ryan Bahillo 3 months ago +1

    Nice video👽. It really help me a lot in my course. Thanks.

  • shravya jalli
    shravya jalli 3 months ago

    are these(lists,tuples,sets) part of the collection datatypes?

  • Amaan Durrani
    Amaan Durrani 4 months ago

    Check this video for lists in python with examples and elaboration
    thexvid.com/video/KooGx1dNq6Y/video.html

  • smithn
    smithn 4 months ago +2

    Listening to you at 1.25x speed was a lot of fun. Thanks for the video. That was very helpful!

  • Reza Sj
    Reza Sj 4 months ago +1

    Just Amazing!

  • Mehmet Mustafa ICER
    Mehmet Mustafa ICER 4 months ago

    Can you please state IDE you are using?

    • Corey Schafer
      Corey Schafer  4 months ago

      Hey There. This is Sublime Text. I made a video on how I set up this Python development environment. You can find that video here:
      thexvid.com/video/xFciV6Ew5r4/video.html
      Thanks!

  • Shafi Syed
    Shafi Syed 4 months ago

    which compiles is this one?

    • Corey Schafer
      Corey Schafer  4 months ago

      Hey There. This is Sublime Text. I made a video on how I set up this Python development environment. You can find that video here:
      thexvid.com/video/xFciV6Ew5r4/video.html
      Thanks!

  • Soflo Antonio
    Soflo Antonio 4 months ago +1

    great tutorial

  • Ano Nymous
    Ano Nymous 4 months ago

    in about 2 days i'll be done with the book automate the boring stuff with python and i'm looking for more literature to learn with. What i liked about it was that there were always examples and tasks for the newly learned material to apply directly what i had learned so far. Can you recommend something similar? Maybe one level higher than beginner.

  • Chary BG
    Chary BG 4 months ago

    Hey Corey,
    When I was running Sets, how come my results were including the word set in it?
    Thnx in advance!
    "set(['Art', 'CompSci', 'Design', 'Physics', 'Math', 'History'])
    [Finished in 0.1s]"

  • Solomon Wogayehu
    Solomon Wogayehu 4 months ago +1

    Great job. I liked your presentation. Very informative. Thank you

  • Kunal Agarwal
    Kunal Agarwal 4 months ago +1

    Nice one

  • Michael P
    Michael P 4 months ago +1

    Thank you for this video Corey! You are one of the best Python teachers on youtube.

  • 0SakA
    0SakA 4 months ago

    which shortcut do you use when you comment out and uncomment out the code after you mark it? I read somewhere is that you need to set this up in python 3. Thank you Corey for the wonderful and useful content.

  • Leeza Rodriguez
    Leeza Rodriguez 4 months ago

    I laughed that the top age bracket is 45 and up.

  • Tahir Sultanov
    Tahir Sultanov 5 months ago +4

    There a lot of professional programmers, but not all are good teachers. You are a great teacher too! So easy to follow up!!! Thanks a lot!!!

  • Cartoons For Kids
    Cartoons For Kids 5 months ago

    Hey! I wanted to know if there was a way to sort a list in reverse using the sorted(x) function

  • NS TK
    NS TK 5 months ago +2

    i should have found this channel earlier ....... I'm going to finish all Python tutorials.... thank you so much!!!!!!!!!!!!

  • Glaurung the Great Worm
    Glaurung the Great Worm 5 months ago +4

    When I hit the like, it changed from 5,1k to 5,2k. Always wanted to be that "one in a thousand"! lol

  • Satyahang Rai
    Satyahang Rai 5 months ago

    plz give me the link of compiler you are using!

    • Corey Schafer
      Corey Schafer  5 months ago

      Hey There. This is Sublime Text. I made a video on how I set up this Python development environment. You can find that video here:
      thexvid.com/video/xFciV6Ew5r4/video.html
      Thanks!

  • Mahesh Shetty
    Mahesh Shetty 5 months ago

    Can we insert the new list at starting position using extend command

  • 3.P Training
    3.P Training 5 months ago +1

    I'm a newbie in python and it's incredible believe that i found this seri, thanks so much!

  • xyz122
    xyz122 5 months ago

    also can you make more videos on advanced topics

  • xyz122
    xyz122 5 months ago

    i know python well sort of ... coming from c++ background but going through these tutorials again gives me better understanding of the language
    thank you

  • Andrés Arenas Vélez
    Andrés Arenas Vélez 5 months ago

    In the List example would be better to explain that when you assign L2 = L1, basically L1 and L2 are the same object. To copy (or clone) L1 into a new object you should use slices , like L2 = L1[:]. Try this two commands : print(a == b) , print(a is b). On the assignment L2=L1, both return TRUE. On the second case (the clone) the first one is true but the second is FALSE.

  • EdwardHal
    EdwardHal 5 months ago +11

    Massive upvote for the Oxford comma in the video title!
    But seriously...great video!

    • Astrix
      Astrix 4 months ago +1

      @No No yep correct xD

    • No No
      No No 4 months ago +1

      @Astrix double negative = positive.

    • Astrix
      Astrix 4 months ago

      @No No no no

    • No No
      No No 4 months ago

      Anyone who has at least had a little bit of education should be aware of that, no?

  • Ankit Kumar
    Ankit Kumar 5 months ago

    I tried doing this:
    courses = ['math','physics','chemistry','english','CompSci']
    print(courses[2:5])
    Output: ['chemistry','english','CmpSci']
    I wish to know why the concept of "Index out of bound" doesn't hold valid here?

    • Ankit Kumar
      Ankit Kumar 5 months ago

      @Corey Schafer Got it. Thanks for the explanation!

    • Corey Schafer
      Corey Schafer  5 months ago +1

      Slicing will simply go to the end of the list if you specify a higher value. If you access the index directly then it will be out of bounds.

  • Miguel Bustos II
    Miguel Bustos II 5 months ago

    So I'm up to the 4:00 minute marker and I'm just now realizing that this is similar to your Intro to Slicing video. Thank you so much for these great videos!!!!

  • Prem Chand
    Prem Chand 5 months ago

    I am getting 'set()' as a result while running print(cs_courses.difference(art_course)). Could you please explain why i am not getting the intersection of two sets

  • TAM15
    TAM15 5 months ago

    what are the benefits of using empty tuples..?

  • Rajeev Patil
    Rajeev Patil 5 months ago +1

    BEST EVER!

  • Shibu Nagendran
    Shibu Nagendran 5 months ago

    If tuples are immutable...then would we create an empty one?

    • Corey Schafer
      Corey Schafer  5 months ago

      Some functions return tuples. If there are no results then you may want to return an empty tuple. I’m sure there may be other examples other than this, but that is one off the top of my head

  • samhouston2000
    samhouston2000 5 months ago +1

    You are awesome for old timers like me, don't need basics just syntax explanations been there and done pretty much everything. Excellent. Keep it up.

  • James wall
    James wall 5 months ago

    Awesome tutorials .. I can't stress enough how awesome they are .. I gotta hand it to you man !!!!!

  • Edward Weinberg
    Edward Weinberg 5 months ago

    Is there an easy way to comment/uncomment blocks of code?

  • Santosh Lohar
    Santosh Lohar 6 months ago +1

    very clasic way of presentation ..thanks

  • nahom alem
    nahom alem 6 months ago +1

    Excellent Job! Keep it up. Really enjoy watching your tutorials.

  • Ryan Stewart
    Ryan Stewart 6 months ago

    Hey this may be a dumb question but, I was wondering how you made your output bar bigger than what atom has it normally set to?

    • Corey Schafer
      Corey Schafer  6 months ago +1

      I'm using Sublime Text in this video. I believe I just changed that in the User Preferences

  • Kabiraj Adhikari
    Kabiraj Adhikari 6 months ago +1

    looking for some one like..
    visit Nepal I'll give you treat..
    Thanks for the videos sir.

  • burak catalok
    burak catalok 6 months ago +1

    Best courses

  • peyman aghaee
    peyman aghaee 6 months ago

    I tried below line instead of "sorted_Course = sorted(Course)" but it doesn't work:
    #sorted_Course = course.sort()
    don't we have such a thing? like Popped = Course.pop() as an example.

  • syed shah
    syed shah 6 months ago +1

    my exam is next week and you just make everything very easy thank-you please keep making good videos like these:)

  • Angus Armstrong
    Angus Armstrong 6 months ago

    I'm coming from MatLab and am curious why indexing in Python starts at 0? What is the reasoning/advantages of this?

    • Angus Armstrong
      Angus Armstrong 6 months ago

      @Corey Schafer Thanks a lot!
      Also, your videos have been a great help in transferring over. I thought the transition would be a lot easier than it is.

    • Corey Schafer
      Corey Schafer  6 months ago

      This is standard in most programming languages. I have read a few justifications over there years. Here is a StackOverflow answering that question for the C Programming language:
      stackoverflow.com/questions/7320686/why-does-the-indexing-start-with-zero-in-c
      The top answer also links to an article explaining in more mathematical detail why this was chosen.

  • Jelmer Sijbranda
    Jelmer Sijbranda 6 months ago

    what version of phython is he Using?

    • Corey Schafer
      Corey Schafer  6 months ago

      I believe I wrote this series in Python 3.6.

  • lucifer reficul
    lucifer reficul 7 months ago +1

    A love it; thanks so much, it will help a lot of students and beginners

  • Xudong Zhang
    Xudong Zhang 7 months ago

    Great video. A little bit confused at 12:40. why the "sorted" function doesn't sort the list in place ?

  • Gary Dorman
    Gary Dorman 7 months ago

    Have you ever thought about adding Pandas tutorials?

  • Robert Anglos
    Robert Anglos 7 months ago

    I get creating an empty mutable collection data type but what is the practical use of an empty Tuple?

  • Allin Man
    Allin Man 7 months ago

    Hello, I wonder why when I try to create an empty set, it shows me that " 'set' object is not callable" Thank you

    • Allin Man
      Allin Man 7 months ago

      ​@Corey Schafer Thanks a lot! That was the problem, I didn't delete the previous "set".

    • Corey Schafer
      Corey Schafer  7 months ago +1

      Hey there. Can you post all of your code? Here is my guess of what is happening... Do you have a variable named "set" somewhere? For example, something like this:
      set = {1,2,3}

      If so, then you are overwriting the built-in set function and it will no longer be able to used as it normally is and will throw the error you gave.

  • Allin Man
    Allin Man 7 months ago

    This is a treasure place. Thank you so much for creating such great content and share it for free! I watched a few OOP series videos and found them really helpful and informative. So I came back to solid my skills by watching every video here. This can really help people who cannot afford University but can find a great job. You are amazing.

  • Sandy
    Sandy 7 months ago

    How can I remove an item from a list that is within a list with the remove function? For hence remove "Education" from this list [['Art', 'Education'], 'Math', 'History', 'Art', 'Science', 'English', 'Gymnastics']

  • Meenal Yengul
    Meenal Yengul 7 months ago

    Hey corey can u plz tell me how to prepare my last interview round where i will be writing python code on spot. how and where should I practice ? if any one knows plz help me thanks in advance

  • Pandit Nandlal Patherwali
    Pandit Nandlal Patherwali 8 months ago +1

    How can we sort a list without using sort() or sorted () function , please help.

    • Gt Cline
      Gt Cline 4 months ago

      Why not just use sort()/sorted()?

  • Pain, Suffering, Death, & Hell before my eyes

    Tuples and sets starts at 20:05