Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask question.

Forgot Password?

Need An Account, Sign Up Here
Sign InSign Up

Stackorigin – The Community of Question and Answers

Stackorigin – The Community of Question and Answers Logo Stackorigin – The Community of Question and Answers Logo
Search
Ask A Question

Mobile menu

Close
Ask a Question

Python built-in Collections

Python has built-in collections those are

ListTuple
SetDict

Python List:

  • List is Insert order is preserved
  • List is Duplicate values allowed
  • List is Heterogeneous (or) Different type value’s allowed
  • List is Grow able ( we can add/remove/update in list)
  • List is declared in square ([]) brackets
  • List values/objects will access by using Index
  • List is mutable.
  1. Empty List: To create empty list. L = []
  2. If we know elements already, We need to implement list. L = [10,20,30].
  3. Nested List: To create nested list.
    • L = [10,20,[30,40,50]]
    • print(L[2][1]) => 40
  4. Traverse of Elements:
    • l = [1,2,3,4,5,6]
    • Using For loop:
    • for x in l:
    • print(x) => 1,2,3,4,5,6
    • Using While loop:
    • i = 0
    • while i < len(l):
    • print(l[i])
    • i =i+1
  5. to get list of length need use len()
    • l = [1,2,3,4]
    • print(len(l)) => 3
  6. Count is count the number of object is there same.
    • l = [1,2,3,4,2,4]
    • print(l.count(4)) => 2
  7. Finding the index of an item in a list
    • l = [1,2,3,4]
    • print(l.index(1)] => 0
  8. Append(): Append useful to add values at end position.
    • l = []
    • l.append[10]
    • l.append[20]
    • print(l) => [10,20]
  9. Insert(): Insert useful to add values at specified position.
    • insert(index, value)
    • l = [1,2,3,4]
    • l.insert(1,200)
    • print(l) => [1,200,3,4]
  10. Extend(): extend() method adds all the elements of an iterable (list, tuple, string etc.) to the end of the list.
    • l = [1,2,3]
    • l2 = [4,5,6]
    • l.extend(l2) => print(l) => [1,2,3,4,5,6]
    • l.extend(‘stack’) =>print(l) => [1,2,3,’s’,’t’,’a’,’c’,’k’]
  11. Remove(): Remove method will remove the element at specified position and this method wont return anything. if value not there Value error will raise.
    • l = [1,2,3,4,5,6]
    • l.remove(1) => print(l) = >[2,3,4,5,6]
  12. pop(): to remove and return last element or using index. if elemets not there in list Index error will raise.
    • l = [1,2,3,4,5] => z = l.pop() => print(z) => [1,2,3,4]
  13. Reverse(): reverse method reverses the elements of the list.
    • l = [1,8,3,0]
    • l.reverse()
    • print(l) = > [0,1,3,8]
  14. Sort(): For sort method we should use homogeneous object (same object). By using sort we can arrange the elements asc/desc order.
    • l = [1,5,2,7]
    • l.sort() => print(l) => [1,2,5,7] => ASC order
    • l.sort(reverse = True) => [7,5,2,1] => DESC Order
  15. List Concatenation: we can perform the concatenate between two list’s. concatenate  will use + operator.
    • l1 = [1,2,3,4] and l2 = [5,6,7,8]
    • c = l1+l2 => print(c) => [1,2,3,4,5,6,7,8]
  16. List Replication: we can perform the replication between two list’s. Replication will use * operator.
    • l = [1,2,3]
    • print(l*2) => [1,2,3,1,2,3]
  17. Clear(): to clear all elements in list.
    • l = [1,2,3,4] => l.clear() => print(l) => []
  18. Membership operator in List:
    • l = [1,2,3,4]
    • print(100 in l) => False
    • print(10 in l) => True
    • print(20 not in l) => False
    • print(200 not in l) => True
  19. List Comprehension: The syntax of List comprehension.
    • [expression for x in sequence if condition]
    • l = [1,2,3,4,5,6,7,8]
    • l2 = [x for x in l if x%2==0]
    • print(l2) => [2,4,6,8]

Python Tuple:

  • Tuple represent parenthesis ().
  • Tuple is Duplicate values allowed
  • Tuple is Heterogeneous (or) Different type value’s allowed
  • Tuple values/objects will access by using Index
  • List is Immutable.
  • We can write tuple without using parenthesis ().

t = 10,20,30

print(type(t)) => tuple

  1. How can represent single value in tuple: t = (“a”,)
  2. Create empty tuple: t = ()
  3. Convert list to tuple
    • l = [1,2,3,4]
    • t = tuple(l)
    • print(t) => (1,2,3,4)
  4. Tuple Concatenation: we can perform the concatenate between two Tuple’s. concatenate  will use + operator.
    • t1 = (1,2,3,4)
    • t2 = (10,20,30,40)
    • t3 = t1 + t2
    • print(t3) => (1,2,3,4,10,20,30,40)
  5. Tuple Replication: we can perform the replication between two Tuple’s. Replication will use * operator.
    • t1 = (1,2,3)
    • t2 = t1 * 2
    • print(t2) => (1,2,3,1,2,3)
  6. to get tuple of length need use len()
    • t = (1,2,3,4)
    • print(len(t)) => 4
  7. Count is count the number of object is there same.
    • t = (1,2,3,1)
    • print(t.count(1)) => 1
  8. Finding the index of an item in a tuple.
    • t = (1,2,3)
    • t.index(1) => 0
    • t.index(900) => value error
  9. Sorted(): By using sorted we can arrange the elements asc/desc order.
    • t = (5,1,2,7)
    • printsorted(t)) => (1,2,5,7)
  10. Find min and ma value in tuple
    • t = (10,1,0,5,100)
    • print(min(t)) => 0
    • print(max(t)) => 100
  11. Tuple Packing:
    • Grouping elements into single
    • a = 10, b = 20, c = 30
    • t = a,b,c
    • print(t) => 10,20,30
  12. Tuple Unpack:
    • t = (1,2,3)
    • a,b,c = t
    • print(a,b,c) => 1,2,3

Difference between List and Tuple:

ListTuple
Elements contain []Elements contain ()
[] its mandatory() its not mandatory
Comprehension is thereComprehension not there
List objects cant be used as keys
for the dictionary
Tuple objects can be use as keys
for the dictionary

Python Set:

  • Duplicate value’s not allowed.
  • Order is not preserved.
    • s = {2,6,3,9}
    • print(s) => {9,2,3,6}
  • Heterogeneous allowed.
  • Set is mutable.
  • Set doesn’t support index.
    • s = {0,1,5,2,4}
    • print(s[0]) => set doesn’t support index.
  • Create empty set.
    • s = set()
    • print(s)
  • convert list, tuple, string to set
    • t = (10,20,30,40) => tuple
    • y = set(s)
    • print(y) => {10,20,30,40}
    • l = [10,20,30,40]
    • m = set(l) => list
    • print(m) => {10,20,30,40}
    • s = “str” => string
    • print(set(s)) => {“st”,”t”,”r”}
  • add(): By using add() we can add only one item at a time.
    • a=set()
    • a.add(10)
    • a.add(20)
    • print(a) => {10,20}
  • Update(): Here we can add able to add multiple elements at a time.
    • s = {9,1,4,2}
    • s2 = [8,0]
    • s.update(s2)
    • print(s) => {9,8,1,4,0,2}
  • POP(): to remove random elements
    • a = {1,5,2,7,3}
    • a.pop()
    • print(a) = >{1,5,2,3}
  • Remove():to remove particular element.
    • a = {1,5,2,7,3}
    • print(a.remove(5))
  • Set Comprehension:
    • s = {x for x in range(1,6) if x%2=0}
    • print(s) => {2,4}

Python Dictionary:

  • Dictionary container key and value.
  • Duplicate keys not allowed and value’s allowed.
  • Heterogeneous allowed.
    • d[key] = value
    • d = {}
    • d[‘sno’] = 100
    • d[‘name’]= “stackorigin”
    • print(d) => {‘sno’:100,”name”:”stackorigin”}
  • Access elements in dictionary
    • d = {‘sno’:100,”name”:”stackorigin”}
    • print(d[‘sno’]) => 100
  • Update dictionary value
    • s = {‘sno’:100,”name”:”stackorigin”}
    • s[‘sno’]= 1
    • print(s) => {‘sno’:1,”name”:”stackorigin”}
  • Delete dictionary key
    • s = {‘sno’:100,”name”:”stackorigin”}
    • del s[‘sno’]
  • Get() get element in dictionary
    • d = {‘sno’:100,”name”:”stackorigin”}
    • print(d.get(‘sno’)) => 100
    • If key not found error will raise
    • so we need pass default value always
    • s={‘sno’:100,”name”:”stackorigin”}
    • print(s.get(“marks”), ‘sno’) => 100
  • s={‘sno’:100,”name”:”stackorigin”}
  • print(s.keys()) => keys will get
  • print(s.values()) => values will get
Share
  • Facebook

Sidebar

Ask A Question

Stats

  • Questions 1k
  • Answers 1k
  • Best Answers 80
  • Users 81

Adv 250x250

Adv 234x60

  • Recent
  • Answers
  • ashok

    How do I get the current time?

    • 2 Answers
  • ashok

    How to Install and Use Git on Windows

    • 1 Answer
  • ashok

    How to Check Git Version

    • 2 Answers
  • arjun

    Which institute is best for a data science course in ...

    • 1 Answer
  • ashok

    git stash and git pull

    • 1 Answer
  • pratap
    pratap added an answer How do I get the current time in Python: The time module… June 29, 2022 at 9:08 am
  • pratap
    pratap added an answer How do I get the current time in Python: Use datetime:… June 29, 2022 at 9:04 am
  • hari
    hari added an answer How to Install and Use Git on Windows Git is… June 29, 2022 at 8:24 am
  • sam
    sam added an answer How to Check Git Version: $ git --version git version… June 29, 2022 at 8:13 am
  • sam
    sam added an answer How to Check Git Version: In a command prompt: $… June 29, 2022 at 8:13 am

New Members

Sakshigupta1998

Sakshigupta1998

  • 0 Questions
  • 0 Answers
legal heir certificate online apply

legal heir certificate online apply

  • 0 Questions
  • 0 Answers
Mutual divorce process india

Mutual divorce process india

  • 0 Questions
  • 0 Answers
propertyregistration

propertyregistration

  • 0 Questions
  • 0 Answers
Best Gynecologist Hospital in Delhi

Best Gynecologist Hospital in Delhi

  • 0 Questions
  • 0 Answers

Adv 234x60

Trending Categories

Programming
825Questions
, 0Followers
Technology
209Questions
, 3Followers
General Knowledge
127Questions
, 0Followers
Business & Finance
81Questions
, 4Followers
Employment
73Questions
, 3Followers

Trending Tags

django (95) git (57) google (29) india (35) mysql (22) oil (54) pandas (23) python (241) usa (25) youtube (24)

Recent posts

    • On: June 29, 2022

    Bitbucket Cloud recently stopped supporting account passwords for Git authentication

    • On: June 21, 2022

    Best Telegram Movie Channels

    • On: June 17, 2022

    How to Open ICICI Bank Savings Account

    • On: June 15, 2022

    How to Download Your Own Twitch Videos

    • On: June 14, 2022

    How To Permanently Delete Your PSN Account

Explore Our Blog

Adv 234x60

Subscribe

Explore

  • Recent Questions
  • Most Answered
  • Answers
  • No Answers
  • Most Visited
  • Most Voted
  • Random

Footer

Stackorigin - The Community of Question and Answers

Stackorigin

Stackorigin is the world’s largest Q&A networking site, Stackorigin community brings you the collaboration of all the various Questions and the related Answers given by the community.

About

  • About Us
  • Contact Us
  • FAQ
  • Submit Guest Post Article on Technology, Education, Health, Apps, Gadgets, IoT, AI, Business, Digital Marketing and More

Info

  • Privacy Policy
  • Terms and Conditions
  • Community Guidelines
  • Tags

Products

  • Tutorials
  • Advertising
  • Categories
  • Corona
  • StackHow

Follow

© 2022 Stackorigin. All Rights Reserved.