Software engineering and personal development

Tag: list (Page 2 of 3)

How to Quickly Simply “if” Statements in Python

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 reading

How to Quickly Check Whether a List Contains Duplicate Elements in Python

white and yellow daisy in bloom
Photo by Stefan Cosma on Unsplash

Sometimes, 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 reading

How to Quickly Convert 2 Lists Into a Dictionary in Python

Get a driver's-eye view of other cars driving along the 110 Freeway in Los Angeles as they pass under the soaring interchange with the 105 Freeway. This is quintessential L.A. architecture and a typical view for thousands of people every commute.  Another sunny day in paradise.
Photo by Chris Linnett on Unsplash

Two 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 reading
« Older posts Newer posts »

© 2024 Fatos Morina

Theme by Anders NorenUp ↑