Leetcode with dani – Telegram
Leetcode with dani
1.31K subscribers
197 photos
14 videos
56 files
240 links
Join us and let's tackle leet code questions together: improve your problem-solving skills
Preparing for coding interviews
learning new algorithms and data structures
connect with other coding enthusiasts
Download Telegram
In Python, dictionaries are mutable data structures that allow you to store key-value pairs. One of the advantages of dictionaries is the ability to add and remove items dynamically.

To add an item to a dictionary, you can simply assign a value to a new or existing key. For example:

my_dict = {"apple": 3, "banana": 5}
my_dict["orange"] = 2


In this example, we added a new key-value pair "orange": 2 to the dictionary my_dict. If the key already exists, the value will be updated; otherwise, a new key-value pair will be created.

To remove an item from a dictionary, you can use the del keyword followed by the key you want to remove. For example:

my_dict = {"apple": 3, "banana": 5, "orange": 2}
del my_dict["banana"]


In this example, we removed the key-value pair "banana": 5 from the dictionary my_dict using the del keyword.

It's important to note that if you try to remove a key that doesn't exist in the dictionary, a KeyError will be raised. To avoid this, you can use the dict.pop() method, which removes the item with the specified key and returns its value. For example:

my_dict = {"apple": 3, "banana": 5, "orange": 2}
removed_value = my_dict.pop("banana")


In this example, the key-value pair "banana": 5 is removed from the dictionary my_dict, and the value 5 is assigned to the variable removed_value.

Remember, dictionaries in Python are unordered, so the order of the items may not be the same as the order in which they were added.
👍32
Leetcode with dani pinned «esti channelun ke10 rate yestu»
I used this book to teach my students and i recommended that for you
Can a dictionary have multiple values for the same key?
Anonymous Quiz
45%
yes
55%
no
👍5
You can check if a key exists in a dictionary by using the in keyword. It returns True if the key is present, and False otherwise. For example:

my_dict = {"dani": 3, "yohanes": 5}
print("eyob" in my_dict) # Output: False
my_dict = {"dani": 3, "eyob": 5, "bisrat": 2}

1.print(my_dict["bisrat"])
2.print("yohannes" in my_dict)
3.my_dict["dani"] = 4
4.print(len(my_dict))
5.del my_dict["eyob"]

Now, based on the code, can you answer the following questions about the dictionary?
1👍1
1.output of no.1
Anonymous Quiz
87%
2
8%
3
4%
5
👍1👏1
2.output of no.2
Anonymous Quiz
79%
False
12%
false
8%
True
1%
true
👍2
3
4.output of no.4
Anonymous Quiz
9%
6
81%
3
10%
4
5. what will be the output of the following code after the no.5 code run? print(my_dict)
Anonymous Quiz
9%
{"dani": 3, "eyob": 5, "bisrat": 2}
86%
{"dani": 3, "bisrat": 2}
5%
my_dict
Leetcode with dani
Can a dictionary have multiple values for the same key?
dictionary in Python cannot have multiple values for the same key. Each key in a dictionary must be unique, and it can only map to a single value.

If you try to assign a new value to an existing key in a dictionary, it will overwrite the previous value associated with that key. Here's an example:

my_dict = {"dani": 3, "abebe": 5, "yonas": 2}

my_dict["dani"] = 7 my_dict["kebe"] = 9

print(my_dict)


Output:
{'dani': 7, 'abebe': 5, 'yonas': 2, 'kebe': 9}


As you can see, the value associated with the key "dani" has been updated from 3 to 7. The dictionary does not store multiple values for the same key.
👍71
Set in python
A set is an unordered collection of unique elements in Python. It is denoted by curly braces ({}) or by using the set() function. Sets are useful when you want to store a collection of items without any duplicates and perform operations like union, intersection, and difference.

Creating a set:
# Creating an empty set
my_set = set()
print(my_set)  # Output: set()

# Creating a set with initial values
my_set = {1, 2, 3, 4, 5}
print(my_set)  # Output: {1, 2, 3, 4, 5}


Adding elements to a set:
my_set = {1, 2, 3}
my_set.add(4)
print(my_set)  # Output: {1, 2, 3, 4}

# Adding multiple elements using update()
my_set.update([5, 6, 7])
print(my_set)  # Output: {1, 2, 3, 4, 5, 6, 7}


Removing elements from a set:
my_set = {1, 2, 3, 4, 5}
my_set.remove(3)
print(my_set)  # Output: {1, 2, 4, 5}

# Removing an element that doesn't exist will raise an error
my_set.remove(6)  # Raises KeyError

# Alternatively, you can use discard() to avoid errors
my_set.discard(6)  # No error raised


Set operations:
set1 = {1, 2, 3}
set2 = {3, 4, 5}

# Union of two sets
union_set = set1.union(set2)
print(union_set)  # Output: {1, 2, 3, 4, 5}

# Intersection of two sets
intersection_set = set1.intersection(set2)
print(intersection_set)  # Output: {3}

# Difference between two sets
difference_set = set1.difference(set2)
print(difference_set)  # Output: {1, 2}


Sets are mutable, meaning you can modify them by adding or removing elements.
👍5
The similarities and differences between list, tuple, and set

1. Lists:
- Lists are ordered collections of items that can be modified (mutable).
- Elements in a list are enclosed in square brackets ().
- Lists allow duplicate elements.
- Elements in a list can be accessed using indexing and slicing.
- Lists are commonly used when you need to store and manipulate a collection of items that may change over time.

Example:
my_list = [1, 2, 3, 4, 5]


2. Tuples:
- Tuples are ordered collections of items that cannot be modified (immutable).
- Elements in a tuple are enclosed in parentheses ().
- Tuples allow duplicate elements.
- Elements in a tuple can be accessed using indexing and slicing, similar to lists.
- Tuples are commonly used when you want to store a collection of items that should not be changed, such as coordinates or database records.

Example:
my_tuple = (1, 2, 3, 4, 5)


3. Sets:
- Sets are unordered collections of unique elements.
- Elements in a set are enclosed in curly braces ({}) or created using the set() function.
- Sets do not allow duplicate elements. If you try to add a duplicate element, it will be ignored.
- Elements in a set cannot be accessed using indexing or slicing since they are unordered.
- Sets are commonly used when you need to store a collection of unique items and perform operations like union, intersection, and difference.

Example:
my_set = {1, 2, 3, 4, 5}


In summary, lists are mutable and allow duplicates, tuples are immutable and allow duplicates, while sets are mutable, unordered, and do not allow duplicates. The choice between them depends on the specific requirements of your program.
👍61🆒1
1. Which of the following collection types in Python is mutable?
Anonymous Quiz
23%
List
10%
Tuple
8%
Set
59%
A and C
2. What is the output of the following code snippet?

my_set = {1, 2, 3} my_set.add([4, 5]) print(my_set)
Anonymous Quiz
46%
{1, 2, 3, [4, 5]}
9%
{1, 2, 3, (4, 5)}
24%
TypeError: unhashable type: 'list'
21%
TypeError: unhashable type: 'set'
👍5
3. What is the output of the following code snippet?

set1 = {1, 2, 3} set2 = {3, 4, 5} result = set1 ^ set2 print(result)
Anonymous Quiz
8%
{1, 2}
4%
{2, 4, 5}
73%
{1, 2, 3, 4, 5}
15%
{1, 2, 4, 5}
👍1👏1
4. How do you check if an element exists in a list called my_list in Python?
Anonymous Quiz
36%
5 in my_list
17%
my_list.contains(5)
18%
my_list.exists(5)
29%
my_list.check(5)
🔥2👍1
6. Which of the following methods is used to clear all elements from a set in Python?
Anonymous Quiz
44%
clear()
23%
remove_all()
22%
delete_all()
11%
discard_all()
🌚2