[Python!] - Set & Dictionary / List & Tuple

Hailey Park·2021년 11월 7일
0

Python!

목록 보기
3/11
post-thumbnail

1. List and Tuple

[The Commonalities]

  • A list is a collection of ordered data.
  • A tuple is an ordered collection of data.
  • The count() method returns the number of times the specified element appears in the list.
  • The count() method returns the number of times a specified value occurs in a tuple.
  • The reverse() method reverses the elements of the list.
  • The reverse() method is not defined in tuples, as they are unchangeable

[The differences]

  • Lists are mutable.
  • Tuples are immutable.
  • Lists are declared with square braces.
  • Tuples are enclosed within parenthesis.
  • The append() method adds a single item at the end of the list without modifying the original list.
  • An element cannot be added to the tuple as it is immutable.
  • The pop() method removes the item at the given index from the list and returns it.
  • Tuples are immutable.
list1=["apple","banana","grapes"]
list1.append("strawberry")   # strawberry is added to the list
print(list1)
list1.pop()  # removes the last element from the list
print(list1)
list1.pop()
print(list1)
tuple1=(1,2,3,4)
# tuple1.pop()    tuple cannot be modified
# tuple1.append()  tuple cannot be modified
print(tuple1)
  • The sort() method sorts the elements of a given list in a specific ascending or descending order.
  • Though tuples are ordered, the elements cannot be sorted.
  • index() searches for a given element from the start of the list and returns the lowest index where the element appears
  • Searches the tuple for a specified value and returns the position of where it was found.
list1=[1,5,3,9,"apple"]
print(list1.index(9)) # returns the index value of the particular element
list2=[2,7,8,7]
print(list2.index(7)) # returns the index value of the element at its first occurence
print(list2.index(7,2)) # returns the index value of the element from the particular start position given


tuple1=(1,3,6,7,9,10)
print(tuple1.index(6))
print(tuple1.index(9))

2. Set and Dictionary

[The Commonalities]

  • A set is an unordered collection.
  • A dictionary is an unordered collection of data that stores data in key-value pairs.
  • Sets are mutable and have no duplicate elements.
  • Dictionaries are mutable and keys do not allow duplicates.
  • There are no count() methods in sets as they do not allow any duplicates.
  • The count() method is not defined in the dictionary.
  • The sets are unordered, which refrains from applying the reverse() method
  • The elements cannot be reversed, as the items in the dictionary are in the form of key-value pairs

[The differences]

  • Sets are represented in curly brackets.
  • Dictionaries are enclosed in curly brackets in the form of key-value pairs.
  • The set add() method adds a given element to a set.
  • The update() method updates the dictionary with the specified key-value pairs
set1={"water","air","food"}
set1.add("shelter")   # adds an element to the set at random position
print(set1)
set1.add("clothes")
print(set1)
set1.pop()  # removes random element from the set
print(set1)
dict1={"fruit1":"apple","fruit2":"banana","veg1":"tomato"}
dict1.update({"veg2":"brinjal"})
print(dict1)
dict1.update({"veg3":"chilli"})  # updates the dictionary at the end
print(dict1)
dict1.pop("veg2")
print(dict1)
  • Elements in the set cannot be sorted as they are unordered.
  • sorted() method is used to sort the keys in the dictionary by default.
  • The index of a particular element is not retrieved as they are unordered.
  • The get() method returns the value of the item with the specified key.
set1={1,5,6,3,9}
# set1.index()  will throw an error as they are unordered


dict1={"key1":"value1","key2":"value2"}
# dict1.index("key1") will throw an error
print(dict1.get("key1"))

Resources

https://www.educative.io/edpresso/list-vs-tuple-vs-set-vs-dictionary-in-python

profile
I'm a deeply superficial person.

0개의 댓글