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 a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

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

Adv 120x600

Looking for advertising?

Consider These Things If You Appreciate What stackorigin Does: Stackorigin is a community where you can ask questions and find answers, for free to everyone. We would appreciate it if you bought us a coffee if you liked what we were doing.

Buy me a Coffe

Adv 120x600

  • Random
  • Answers
    • On: August 4, 2022
    • Answers: 0

    required Buy Amoxicillin, Amoxicillin rules

    • On: August 7, 2022
    • Answers: 0

    FIND! Order Tizanidine Online France, Tizanidine hcl side effects

    • On: August 3, 2022
    • Answers: 0

    Naltrexone without prescription fedex, Naltrexone

    • On: August 4, 2022
    • Answers: 0

    Buy Neurontin Online. General Overview of the Drug,

    • On: August 20, 2021
    • Answers: 0

    What's the difference between os.path.abspath and os.path.realpath in Python?

  • strapcart_online
    strapcart_online added an answer Sildenafil citrate is a common ingredient in Cenforce 150mg tablets. This drug… March 21, 2023 at 11:42 am
  • strapcart_online
    strapcart_online added an answer Vilitra 20mg tablets are used to overcome the problem of… March 18, 2023 at 6:13 am
  • ashok
    ashok added an answer Not every girl, there are some girls out there who… March 16, 2023 at 4:15 am
  • strapcart_online
    strapcart_online added an answer Tadarise 40 Tablet is used to permanently remove the problem… March 4, 2023 at 5:48 am
  • strapcart_online
    strapcart_online added an answer Men use Cenforce D tablets for the treatment of sexual… March 1, 2023 at 5:59 am

Adv 120x600

Trending Categories

Health
2041Questions
, 4Followers
Programming
1236Questions
, 0Followers
Technology
216Questions
, 3Followers
General Knowledge
131Questions
, 0Followers
Business & Finance
84Questions
, 4Followers

Adv 120×600

Stats

  • Questions 4k
  • Answers 1k
  • Best Answers 105
  • Users 100

Users

niyadas823

niyadas823

  • 0 Questions
  • 0 Answers
nirala3

nirala3

  • 2 Questions
  • 0 Answers
Perrywalton

Perrywalton

  • 1 Question
  • 0 Answers
veeraa

veeraa

  • 0 Questions
  • 0 Answers
Stewesmitz

Stewesmitz

  • 0 Questions
  • 0 Answers

Adv 120×600

Explore

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

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.