When you have a list of elements, there can be cases when you need to check whether there an element is part of a list or not.
You can do this using in
checker.
Software engineering and personal development
When you have a list of elements, there can be cases when you need to check whether there an element is part of a list or not.
You can do this using in
checker.
There can be cases when we want to check whether a value is equal to one of the multiple other values.
One way that someone may start using is using or
and adding multiple checks. For example, let us say that we want to check whether a number is 11, 55, or 77. Your immediate response may be to do the following:
m = 1
if m == 11 or m == 55 or m == 77:
print("m is equal to 11, 55, or 77")
There is fortunately another quick way to do that.
Continue readingIf you need to find the longest string in a list in Python, you may be tempted to do that in the form of iterating through every element, finding their length, and checking whether this current length is larger than a temporary value that we have chosen in the beginning.
Continue readingSometimes, you may need to check whether a list has any duplicate elements. This can be a common task when you are given a list of numbers or a list of strings. This can also be something that you may need to do both at work, or it can also be part of a coding challenge that you are implementing when being asked at a programming interview.
Continue readingWhen you are given a dictionary, you can do a lot of things with both its keys and its values. From time to time, you may need to do some sort of comparison based on the values of the dictionary. This can include finding the largest value, the smallest value, the sum of all the values, etc.
Continue readingTwo of the most common data structures in Python are lists and dictionaries. If you take a look back at your code, you may notice that you have a lot of lists and dictionaries used all over the place.
They may look quite similar, but they have a key difference. You can think of a list as a collection of items, and a dictionary as a collection of key-value pairs.
There can be cases when you may need to do a conversion of 2 lists into a single dictionary. This is something that you can easily do in Python with the help of zip() function.
Continue readingThere can be cases when you may need to find the difference between 2 sets in Python. If you do not remember from high school, the difference between 2 sets means that we get the elements from the first set that are not in the second set.
We can do that quite easily in Python using the minus operator.
Continue readingLists are pretty much widely used throughout every project that you can think of. Tuples, on the other hand, are not that common, but both of them have something in common that you can use right away.
You can use the same method for both of them to find indexes of certain elements.
Continue readingMost of the time you are adding elements into a list, or set, or a dictionary. However, there can also be cases when you just need to remove elements. Maybe a particular element, or even all elements at once.
If that is the case, then there is a quick method in Python that you can use to do that.
Continue readingLooping through a list in Python is quite straightforward.
Let us mention two ways that you can do it.
You can do it using indexes and the length of the list, like the following:
Continue reading© 2024 Fatos Morina
Theme by Anders Noren — Up ↑