Python Tuple

Accessing The Index Of Iterables In Python

Summary: To access the index of iterables like lists in Python, use one of the following methods: Use enumerate() function. Use a counter variable with For Loop/While Loop. Use a list comprehension. Use the NumPy library. Use the itertools module. Introduction An index can be considered as the position of an element in an ordered …

Accessing The Index Of Iterables In Python Read More »

How to Solve Python “TypeError: ‘int’ object is not iterable”?

It’s quite common for your code to throw a typeerror, especially if you’re just starting out with Python. The reason for this is that the interpreter expects variables of certain types in certain places in the code. We’ll look at a specific example of such an error: “typeerror: ‘int’ object is not iterable”. Exercise: Run …

How to Solve Python “TypeError: ‘int’ object is not iterable”? Read More »

Python One Line Class

Do you hate those lengthy class definitions with __init__ and too many whitespaces and newlines? Python One-Liners to the rescue! Luckily, you can create classes in a single line—and it can even be Pythonic to do so! Sounds too good to be true? Let’s dive right into it! Problem: How to create a Python class …

Python One Line Class Read More »

Python Define Multiple Variables in One Line

In this article, you’ll learn about two variants of this problem. Assign multiple values to multiple variables Assign the same value to multiple variables Let’s have a quick overview of both in our interactive code shell: Exercise: Increase the number of variables to 3 and create a new one-liner! Let’s dive into the two subtopics …

Python Define Multiple Variables in One Line Read More »

Python One Line Conditional Assignment

Problem: How to perform one-line if conditional assignments in Python? Example: Say, you start with the following code. You want to set the value of x to 42 if boo is True, and do nothing otherwise. Let’s dive into the different ways to accomplish this in Python. We start with an overview: Exercise: Run the …

Python One Line Conditional Assignment Read More »

Asterisk Operator

What is the Asterisk / Star Operator (*) in Python?

Many Python coders—even at intermediate skill levels—are often puzzled when it comes to the asterisk character in Python. What does it mean? How does it work? What’s it purpose? This article answers all of those questions and more. After studying this article, you will have a solid understanding of the asterisk operator * in Python—and …

What is the Asterisk / Star Operator (*) in Python? Read More »

The Ultimate Guide To Python Tuples

Python has several built-in data structures such as Lists, Sets and Dictionaries (check out the articles!).In this article, you’ll learn everything you need to know about tuples including real world examples. Tuple Motivating Example Did you ever have a function from which you wanted to return several values? Say, you’ve got a function translate(text, target_language) …

The Ultimate Guide To Python Tuples Read More »

The World’s Most Concise Python Cheat Sheet

Do you want to learn Python but you’re overwhelmed and you don’t know where to start? Learn with Python cheat sheets! They compress the most important information in an easy-to-digest 1-page format. Here’s the new Python cheat sheet I just created—my goal was to make it the world’s most concise Python cheat sheet!

Dict to List — How to Convert a Dictionary to a List in Python

Summary: To convert a dictionary to a list of tuples, use the dict.items() method to obtain an iterable of (key, value) pairs and convert it to a list using the list(…) constructor: list(dict.items()). To modify each key value pair before storing it in the list, you can use the list comprehension statement [(k’, v’) for …

Dict to List — How to Convert a Dictionary to a List in Python Read More »