Trending March 2024 # Python Filter List: 5 Practical Methods Explained # Suggested April 2024 # Top 8 Popular

You are reading the article Python Filter List: 5 Practical Methods Explained updated in March 2024 on the website Hatcungthantuong.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested April 2024 Python Filter List: 5 Practical Methods Explained

An important aspect of data analysis is to get the right data. You need a mechanism to find and extract relevant data. Lists are one of the frequently used data structures and Python offers multiple ways of filtering a list to get relevant data.

In Python, you can filter lists using the built-in `filter()` function, list comprehensions, or ‘for’ loops combined with conditional statements. The `filter()` function applies a specific function to a list and returns an iterable for the elements where the function returned True, list comprehensions provide a concise syntax for creating new lists based on existing ones, and ‘for’ loops can also be used with ‘if’ statements to iterate over a list.

If you’re a beginner, you’ll find yourself confused about choosing the right filter method for your use case. This blog will help you understand 5 ways of filtering a list in Python. Furthermore, you’ll gain knowledge on when to use what method.

Let’s get into it!

In this section, we’ll talk about 5 ways of filtering a list in Python.

We’ll go over the following:

Filtering with built-in filter function

Filtering with list comprehension

Filtering with loops and conditional statements

Filtering with NumPy library

Filtering with Pandas library

Python comes loaded with a range of functions to help make programming more efficient. As you utilize filtering methods in your code, you’ll find that in most cases, the built-in filter() function will be your go-to method for filtering.

The filter() is a built-in Python function that allows you to process an iterable (like a list or tuple) and extract elements that satisfy a specific condition.

The syntax for the filter() function is as follows:

filter(function, iterable)

The function inside filter() is a custom function that tests each element in the iterable to determine if it satisfies the given condition. If the function returns True for an element, it’s included in the output.

The following example shows how to filter elements with the filter() function:

def is_even(x): return x % 2 == 0 my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] result = filter(is_even, my_list) new_list = list(result) print(new_list)

In this example, the built-in filter() function applies the is_even() function to the list elements. The filter() function returns an iterator, which is then converted to a new list containing only the even numbers. The even elements are kept, whereas the odd elements are filtered out.

The filtered list is shown below:

You can also use lambda functions with the filter() function to create more concise and readable code.

The following example demonstrates using lambda expressions with filter() function:

my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9] result = filter(lambda x: x % 2 == 0, my_list) new_list = list(result) print(new_list)

In this example, the lambda expression replaces the is_even() function.

The lambda function returns true if the number is even, else it returns false.

The built-in filter() function takes two arguments: a function and a list.

The filter() function is useful when you have an existing function that you want to apply to a sequence. It’s often used in functional programming styles.

List comprehension allows you to build lists based on existing lists or iterable objects. It allows you to perform operations such as filter in a single line of code.

Suppose we have a list of numbers and we want to create a new list containing the squares of only the even numbers. We can use a list comprehension to achieve this:

numbers = [1, 2, 3, 4, 5, 6] even_squares = [x ** 2 for x in numbers if x % 2 == 0] print(even_squares)

In this example, the expression x ** 2 calculates the square of each element x in numbers. The if condition filters out only even elements, and the final list comprehension returns a new list containing the squares of those even values.

This is a Pythonic way of creating lists based on existing lists. It’s useful when the logic can be implemented in a single line. It’s often recommended when you want concise, readable code and the filtering logic is not overly complicated.

You can also filter a list with loops and conditional statements, but this is generally not preferred in large-scale projects.

To filter a list, we iterate through its elements and apply a specific criterion to each element. If the criterion is met (returns True), the element is added to the output list; otherwise, it’s excluded (returns False).

The following example demonstrates this method:

input_list = [1, 2, 3, 4, 5, 6] output_list = [] for number in input_list: if number % 2 == 0: output_list.append(number) print(output_list) # Output: [2, 4, 6]

In this example, we use a for loop to iterate through the elements in input_list. Inside the loop, we use an if statement to check if the number is even by using the modulus operator (%). If the condition evaluates to True, the number is appended to the output_list.

This is a basic way to filter a list and does not require any additional Python knowledge beyond loops and conditions. It’s easy to understand and can be used in any situation, but it may lead to more verbose code compared to other methods. If you use multiple if-else conditions, your code easily becomes unreadable.

NumPy is used for numerical computations and has support for arrays. You can filter arrays in NumPy using boolean indexing.

An example of filtering with NumPy is given below:

import numpy as np list_1 = [1, 2, 3, 4, 5, 6, 7, 8, 9] numbers = np.array(list_1) evens = numbers[numbers % 2 == 0] print(evens)

In this example, we perform the operation numbers % 2 == 0. This operation is applied to every element in the numbers array which results in a boolean array.

We use this boolean array to index the original numbers array, which effectively filters out the even numbers.

This is useful when you’re dealing with numerical data and you need to perform operations on multi-dimensional arrays. NumPy has powerful and efficient array filtering capabilities, especially for larger datasets. If you are working on numerical computations and performance is a concern, NumPy can be a good choice.

Pandas is widely used for data manipulation and analysis. The filtering of a Pandas Series is similar to the filtering of a NumPy array, but instead of the array, we’re working with a Series object.

The following example demonstrates filtering a list with Pandas library:

import pandas as pd numbers = pd.Series([1, 2, 3, 4, 5, 6, 7, 8, 9]) evens = numbers[numbers % 2 == 0] print(evens)

In this example, we perform the operation numbers % 2 == 0. This operation is applied to every element in the numbers Series resulting in a boolean series.

We use this boolean Series to index the original numbers Series, which filters out the even numbers.

Pandas is great for data manipulation and analysis. It’s especially useful when you are dealing with structured data like CSV, Excel files, or SQL databases.

If your data is more complex than simple lists or your filtering logic involves complex operations like group by, merge, join, etc., then pandas would be the go-to choice.

Filtering is a fundamental operation. Whether you’re handling small data lists or enormous datasets, the ability to filter and extract meaningful information is essential.

Learning these techniques not only broadens your skill set but also prepares you for the varied and often unpredictable nature of real-world programming tasks.

Understanding when and how to use different filtering methods — built-in functions, list comprehension, loops, NumPy, or Pandas — enables you to write more efficient and readable code. Happy coding!

To learn more about how to use Python, check out the playlist below:

You can filter object by using the built-in filter() function. The filter() function accepts two arguments: a filtering function and an iterable (e.g., a list). The filtering function should return either True or False for each element in the iterable.

In the example below, the filter() function returns a new iterable containing only those elements that satisfy the filtering function.

my_objects = [obj1, obj2, obj3, obj4] def filtering_function(obj): filtered_objects = filter(filtering_function, my_objects)

There are multiple ways to filter a list of strings in Python, including using filter() or list comprehensions. The method you choose depends on your specific requirements and coding style preferences.

Here’s an example of list comprehension:

string_list = ["apple", "banana", "cherry", "kiwi"] filtered_list = [x for x in string_list if "a" in x]

You can use the lambda function to create an anonymous filtering function for the filter() function.

For example, if you want to filter a list of integers to include only even numbers, you can use the following one-liner:

int_list = [1, 2, 3, 4, 5, 6] filtered_list = list(filter(lambda x: x % 2 == 0, int_list))

List comprehensions offer a more concise way to filter lists in Python. They consist of an expression followed by a for statement and an optional if statement, all within square brackets.

The following example filters a list of numbers to keep only the positive ones:

numbers = [-3, -2, -1, 0, 1, 2, 3]

To filter a list of dictionaries, you can use list comprehensions or the filter() function.

Here’s an example using list comprehension to filter a list of dictionaries based on the value of a specific key:

dict_list = [ {"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}, {"id": 3, "name": "Charlie"}, ]

Python arrays can be filtered in the same way as lists, using either the filter() function or list comprehensions.

For example, you can filter a NumPy array as follows:

import numpy as np array = np.array([1, 2, 3, 4, 5])

In this example, the array is filtered using boolean indexing to keep only the elements greater than 3.

You're reading Python Filter List: 5 Practical Methods Explained

How To List, Create Python Directory

Introduction to Python Directories

Python directory is defined as a list of folders, files of different types like a tree structure having sub-directories inside the directory and files inside the sub-directories, as we have too many files and directories python directory will come to rescue in managing the files, directories, and sub-directories is called python directory. Python has an OS module that will help manage, create, remove, read and write directories, files, etc. Using it, we can perform many operations using OS modules like creating a directory, getting the current location of a directory, rename a directory, changing the directory, etc.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

How to Create Python Directory?

In Python, Python directory can be created using the OS module and using its function mkdir() in python as below:

Syntax:

os.mkdir('name of the directory')

The directory name’s name is the directory name that will be created in the current working directory location.

Let us create a directory called “repository” in the current working location and another directory called “sdk” in a specific location in the following examples.

Code:

import os os.mkdir('repository') os.mkdir('C:/Users/lenovo/PycharmProjects/sdk')

In the above example, we have imported the os module and using the function mkdir() to create the directory ‘repository’. In another statement, we are creating a directory “sdk” at location ‘C:/Users/lenovo/PycharmProjects’. We need to use forward slash ‘/’ while creating a directory in other locations.

Output:

We will see the list of directories available in the current working directory as below:

Now we will see the output of directories available in the current working directory after the creation of the above directories in the example and output as below:

In the above output, we can able to see the new directories ‘repository’ and ‘sdk’, which we created in the above example.

How to List Python Directories?

In python, directories at any given location can be listed using the OS module, which has many functions and to list is listdir() function.

Syntax:

The syntax to list python directories is as below:

os.listdir()

or

os.listdir('path')

The above command will list all directories in the current working directory, and another syntax will take the path as an argument and list all the directories in that path.

Example:

Let us create an example to list all the directories present in the current working directory that is

“C:/Users/lenovo/PycharmProjects/projman” and using the path same as a current working directory as below:

Code:

import os os.listdir() os.listdir('C:/Users/lenovo/PycharmProjects/projman')

In the above example, we have imported the OS module and using its function listdir() to see the list of available directories in the current working directory and whereas in another example, we are passing path “C:/Users/lenovo/PycharmProjects/projman” to the listdir() function so that it will list available directories in that particular path.

Output:

In the above example, we have executed the os.listdir() function in the current working directory, and the list of directories are ‘Programming’, ‘projman’, ‘projman.rar’,’pycharmprojects’,’repository’, and ‘sdk’.

In the above example, we have given a path to the os.listdir() function and lists all the available directories in that path as ‘.idea’,’Approach’,’build’, ’Makefile’,’README.md’, and ’src’.

How to Create Temporary Python Directories?

In python, we can create the temporary directory using the tempfile module and the function TemporaryDirectory() as below:

Syntax:

The syntax to create the Temporary Directory in python is as below:

import tempfile tempfile.TemporaryDirectory() or tempfile.TemporaryDirectory(dir='path')

In the above syntax, we have imported the module tempfile and using the function TemporaryDirectory() to create a temp directory in the temp directory, and we can pass the path where it needs to create the temporary directory in the second syntax.

Example:

Let’s create an example to create a temporary directory in the temp location and another temporary directory by giving a location as below:

Code:

import tempfile f = tempfile.TemporaryDirectory() f.name

Output:

In the above example, we have imported the tempfile module and created a temporary directory using TemporaryDirectory() function, and to check the temporary directory name, we have used file pointer.“name”, which gives the location of the file with the name as in the above output.

Code:

import tempfile tempfile.TemporaryDirectory(dir ='C:/Users/Lenovo/PycharmProjects')

In the above example, we have imported the tempfile module and explicitly gave the path ‘C:/Users/lenovo/PycharmProjects’ to create the temp file.

Output:

We can observe the temporary file name in the location or path that we have provided the TemporaryDirectory() function in the above output. If we use file pointer, then we need to use chúng tôi otherwise, by default, it will display the temporary file location after creation.

How to Write your Own Python Directories?

In python, we can create our own directories apart from the final directory using the OS module and its function makedirs(), which will create our own directories mentioned in the path.

Syntax:

The syntax to create own python directories is as below:

import os os.makedirs('path')

In the above syntax, we have imported the module OS and using its function mkdirs() with a path that will have a list of directories to be created on its own.

Example:

Let’s create an example to create its own directories based on the path given “sdk/repos/hello” in the current working directory as below:

Code:

import os os.makedirs("sdk/repos/hello")

In the above example, we have imported the OS module and using its function makedirs() to create directories on its own based on the path “sdk/repos/hello” apart from creating “sdk” it will create folders “repos”, “hello” on its own.

Output:

In the above output, we can see the directories created on their own after executing the example can be seen as “sdk”, “repos”, and “hello”.

Advantages of Python Directories

By using python directories, we can manage a large number of files and directories easily.

Python modules OS and tempfile provide different functions to apply to python directories.

They are easy to use and understand.

It provides an efficient way of handling file systems in python.

Conclusion Recommended Articles

We hope that this EDUCBA information on “Python Directories” was beneficial to you. You can view EDUCBA’s recommended articles for more information.

How To Clear Scratch Disk In Photoshop On Windows: 5 Methods

How to Clear Scratch Disk in Photoshop on Windows: 5 Methods Make the best of the temporal storage

440

Share

X

According to Adobe, a working disk is a (virtual) storage drive in your computer that is used temporarily for Photoshop when the program is running.

If you have used Adobe Photoshop on your computer, you’ve encountered the working disk error at least a few times and would need to clear the scratch disk.

X

INSTALL BY CLICKING THE DOWNLOAD FILE

To fix Windows PC system issues, you will need a dedicated tool

Fortect is a tool that does not simply cleans up your PC, but has a repository with several millions of Windows System files stored in their initial version. When your PC encounters a problem, Fortect will fix it for you, by replacing bad files with fresh versions. To fix your current PC issue, here are the steps you need to take:

Download Fortect and install it on your PC.

Start the tool’s scanning process to look for corrupt files that are the source of your problem

Fortect has been downloaded by

0

readers this month.

Photoshop is an incredibly powerful application with great features. But while the program interface is overly intuitive, the same can’t be said about dealing with its bugs.

The most common Photoshop error is that the scratch disk is full. Some users get it when they try to start the program, and others when they try to perform a specific action.

What is a scratch disk?

Here is what you need to know about the photoshop scratch disk:

It is used for storage while photoshop is launched since the running app requires memory to store temporary project files.

If the program doesn’t have enough RAM to store something temporarily, it uses the hard disk space as a temporary virtual memory holder.

This hard disk space is called working volume (scratch disk).

If both the memory and the working volume become full of temporary files, you’ll get an error message that says Scratch Disk is full.

In the following lines, you will find practical solutions to empty the scratch disk in Photoshop.

How do I clear scratch disk in Photoshop on Windows? 1. Free up enough disk space

This is a helpful solution if you are wondering how to clear the scratch disk in Photoshop on Windows devices.

Make sure you have enough space in the drive partition where the working volume is located. The C:/ drive is automatically selected as the working volume if you do not specify it.

2. Delete the temporary files

If you habitually forcibly close the program without giving it enough time to close projects properly, a large portion of the temporary files will be left behind.

If you do this often enough, your temporary folder becomes full of Photoshop files. But these files are easy to identify and delete. Usually, their name starts with PST or Photoshop Temp.

You can safely delete them without affecting the system.

If you’re having problems deleting your temporary files, check out the simple solutions from this handy guide and get rid of them once and for all.

3. Change the location of the working volume

This is a nice shortcut that you can use to change scratch disk settings in case your Photoshop shows you the scratch disk is full before you get into the settings.

4. Increase the amount of memory allowed by Photoshop

This is a helpful solution if you are wondering how to clear the scratch disk in Photoshop on Windows devices.

By default, Photoshop is programmed to use 60% of all RAM. However, you can use it even more, as mentioned above.

5. Customize your working drives

By default, only your C:/ drive is selected as a working volume, but you can allow all your partitions to share this load.

Experienced Photoshop users sometimes set up a special hard disk partition to prevent a problem like this from occurring.

If you were wondering how to clear the scratch disk in Photoshop on Windows devices, we hope this guide has been helpful.

Still experiencing issues?

Was this page helpful?

x

Start a conversation

How To Copy A List In Python? (Why It Fails With Assignment)

In Python, copying a list can be unintuitive for beginners. This is because you cannot use the assignment operator to take an independent copy of a list. This can be confusing especially for beginner Pythonistas.

To create an independent copy of a list in Python, you need to use the copy module’s deepcopy() function like this:

import copy new_list = copy.deepcopy(old_list)

This comprehensive guide teaches you how to take an independent copy of a list in Python. You will also learn why doing it with an assignment operator doesn’t work.

The Problem with Copying Lists in Python

When dealing with Python variables, you can create a copy of a variable by assigning the existing variable to a new one.

For example:

a = 10 b = a

Now the variable b is a copy of variable a.

But if you copy a Python list in a similar fashion:

new_list = old_list

Any modifications made to the new_list also change the original list old_list.

It happens because new_list is actually not a copy of old_list. Instead, it is a reference to the same object in memory.

“Copying” a list just creates an alias for the same object in memory.

To create a completely independent copy of a list, use the copy module’s deepcopy() function.

import copy new_list = copy.deepcopy(old_list)

This is the only rational way to create a fully independent copy of a list in Python.

But if you don’t care about independent copies, there are many other ways to create a copy of a list in Python. These are:

copy() method. Creates a shallow copy.

[:] slicing operator. Creates a shallow copy.

list() function. Creates a shallow copy.

copy.copy() function. Creates a shallow copy.

copy.deepcopy() function. Creates a deep copy.

The following sections teach you how the assignment operator works, what is a shallow copy, and why copying “fails” with the assignment operator.

Assignment Operator (=) in Python

If you use the assignment operator (=) to copy a list in Python, you are not actually creating a copy of the object. Instead, you just give rise to a new variable that refers to the original list. The new variable is an alias to the original list.

Let’s see an example where we:

Create a list.

Assign or “copy” the list to a new variable.

Change the first number in the original list.

Print both lists.

And see what happens.

Here’s the code:

numbers = [1, 2, 3] new_numbers = numbers # Only change the original list numbers[0] = 100 print(numbers) print(new_numbers)

Output:

[100, 2, 3] [100, 2, 3]

Here you only changed the first element in the original numbers list. But this change also took place in the new_numbers list.

This happens because numbers and new_numbers are actually the very same list object.

Under the hood, both lists point to the same blob in memory.

Another way to verify this is by checking the memory address of these objects.

In Python, you can use the id() method to find out the memory address of any object.

Let’s check the memory addresses of both numbers and new_numbers.

print(id(numbers)) print(id(new_numbers))

Output:

140113161420416 140113161420416

The IDs are the same!

This verifies that the numbers and new_numbers are aliases pointing to the same list object in memory.

Think of the list object as a chunk of memory without a name. The numbers and new_numbers are just names via which you can access the list object in memory.

So when you create a new variable and assign a list object to it, you are introducing a new reference label to the original object.

To recap, the assignment operator (=) creates a new reference to an object in memory. It does not copy anything. This applies to lists as well as any other object in Python.

Next, let’s take a look at how you can actually copy list objects in Python.

The Copy Module in Python

As you learned, you cannot use the assignment operator to copy objects in Python. This is why there is a separate module, copy dedicated to copying Python objects.

The two key functions in the copy module are:

copy.copy()

copy.deepcopy()

Let’s take a look at what these functions do and what are the differences.

Shallow Copy: copy.copy()

In Python, a shallow copy can be created using copy.copy() function. A shallow copy solves our problem of copying a list in a way it does not depend on the original list.

For example:

import copy numbers = [1, 2, 3] # Independent copy of 'numbers' list new_numbers = copy.copy(numbers) numbers[0] = 100 print(numbers) print(new_numbers)

Output:

[100, 2, 3] [1, 2, 3]

As you can see, changing the first element in the original list did not change the copied list.

Let’s also verify the objects are not the same by using the id() function:

print(id(numbers)) print(id(new_numbers))

Output:

139764897739904 139764897692480

Horray! Now you know how to create a shallow copy of a list in Python. But mind the word shallow! It’s important to notice that sometimes you might deal with a list of lists.

In this case, the shallow copy does not behave the way you expect. Instead, it creates an independent copy of the outer list, but the inner lists are bound to the original list.

I know it sounds confusing.

Let me show what this means by running a simple experiment in which I:

Create a list of lists.

Create a shallow copy of the list.

Modify the first list’s first object.

import copy numbers = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] new_numbers = copy.copy(numbers) numbers[0][0] = 1000 print(numbers) print(new_numbers)

Output:

[[1000, 2, 3], [4, 5, 6], [7, 8, 9]] [[1000, 2, 3], [4, 5, 6], [7, 8, 9]]

Here changing the first element of the first list affects the copied version of the list even though the new list should be a copy of the original one.

But why does this happen?

Let’s first compare the IDs of the lists to see if they are the same object:

print(id(numbers)) print(id(new_numbers))

Output:

140602923260928 140602923261632

Even the IDs do not match! This means new_numbers should be a true copy of numbers—and it is!

But why do the values still change in the copied list?

This is because copy.copy() creates a shallow copy.

This means the whole list is copied, but the lists inside the list are not. In other words, the inner lists refer to the lists in the original list.

I know this sounds strange, but this is how it works.

Let’s verify this by checking the IDs of the lists inside the list:

print(id(numbers[0]), id(numbers[1]), id(numbers[2])) print(id(new_numbers[0]), id(new_numbers[1]), id(new_numbers[2]))

Output:

140685291558208 140685291498496 140685291708160 140685291558208 140685291498496 140685291708160

As you can see, all the inner list IDs are the same.

So the outer list is copied but the inner lists are still bound to the original list of lists.

To put it together, here is an illustration of how copy.copy() works on a list of lists.

This highlights the behavior of shallow copying in Python.

As stated earlier, to create a completely independent copy, use the copy.deepcopy() function. Let’s take a closer look at this function to see what it does.

Deep Copy: copy.deepcopy()

Another key function in the copy module is the deepcopy() function.

This function creates a completely independent copy of a list or any other compound object in Python.

For example, let’s repeat the example in the previous chapter using deepcopy():

import copy numbers = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] new_numbers = copy.deepcopy(numbers) numbers[0][0] = 1000 print(numbers) print(new_numbers)

Output:

[[1000, 2, 3], [4, 5, 6], [7, 8, 9]] [[1, 2, 3], [4, 5, 6], [7, 8, 9]]

As you can see, changing the first element in the first list did not affect the copied list.

In other words, you have successfully created a completely independent copy of the original list.

No part in the deep-copied list points to the original list. Thus, a deep copy creates a truly independent copy.

I recommend you play with the examples to learn what is happening truly.

Copying a Number Object in Python

This guide would not be complete if we did not talk about copying other objects than lists. It is important to realize everything related to copying lists applies to copying any other Python object.

Let’s repeat the very first example in this guide using integers instead of lists.

In other words, let’s:

Create a number variable.

Copy the number to another variable using the assignment operator.

Change the original number.

See what happens to the copy.

a = 10 b = a a = 50 print(a, b)

Output:

50 10

As you can see, changing the original number a did not change the number b. This is probably something you would expect.

But this contradicts what we said earlier about copying Python objects: A Python object cannot be copied using the assignment operator.

However, looking at the above example, it seems b is an independent copy of a because changing a does not change b.

Even though this happens, b is not a copy of a. This is important to understand. You can verify this by checking the IDs of the variables before changing the value in a.

a = 10 b = a print(id(a)) print(id(b))

Output:

9789280 9789280

As you can see, the IDs match. In other words, a and b are both aliases to the same integer object in memory.

But why does changing a not change b then?

It all boils down to mutability.

First of all, you need to recall that a variable is just a label via which you can access an object that lives somewhere in memory. So if you change the value of a variable, you aren’t actually changing the variable itself, but the object in memory that it refers to.

In Python, integer objects are immutable. Immutability means you cannot make direct changes to integer objects. If you assign a new integer to a variable, you create a new integer object in memory and set the existing variable point to that new memory address.

On the other hand, a list is a mutable object. This means you can change the list object directly. So if you assign an existing list to a new variable, you are making the new variable point to the original list.

This describes Python mutability in a nutshell.

Now, let’s go back to the example of copying an integer. Let’s print the IDs of the variables before and after changing the value in a:

a = 10 b = a print(f"Before assignment id(a) = {id(a)}, id(b) = {id(b)}") a = 50 print(f"After assignment id(a) = {id(a)}, id(b) = {id(b)}")

Output:

Before assignment id(a) = 9789280, id(b) = 9789280 After assignment id(a) = 9790560, id(b) = 9789280

The IDs of variables a and b match before assigning a new value to a but not afterward.

In other words, before changing the value in a:

a and b point to the same integer object in memory.

And after changing the value in a:

a points to a new integer object in memory but b still points to where a used to point.

So after assigning a new value to variable a, it points to a new integer object in memory. This happens because of immutability. The integer object 10 cannot directly change. Instead, a new integer object needs to be created.

Here is a quick illustration of how the code works:

Assigning a new integer to a creates a new integer object where the variable a points to.

To recap, the assignment operator (=) cannot be used to copy objects in Python. However, when dealing with immutable objects, it looks as if this was the case. But it is not.

If someone tells you to copy a variable, technically you need to use copy.copy() or copy.deepcopy() instead of the assignment operator.

However, when dealing with immutable objects, this is unnecessary, as the behavior is the same regardless of whether you used copy module or assignment operator.

But with mutable objects, you need to use the copy module to create a real copy of the object.

At this point, you understand why the assignment operator does not copy objects in Python. You also learned how to use the copy module to create copies of Python objects.

Now that you understand what is a shallow copy and a deep copy, let’s put it all together by taking a look at 5 common ways to copy a list in Python.

5 Ways to Copy a List in Python

There are five main ways to copy a list in Python:

Let’s see examples of each of these

1. The copy() Method

As of Python 3.3, a list comes with a built-in copy() method. This method creates a shallow copy of the list.

For example:

numbers = [1, 2, 3] new_numbers = numbers.copy() print(numbers) print(new_numbers)

Output:

[1, 2, 3] [1, 2, 3] 2. The [:] Slicing operator

In Python, slicing means pulling a range of values from an iterable, such as a list.

Slicing goes with the syntax of:

iterable[start:end]

Where start specifies the starting index and end specifies the ending index.

If you do not specify the start parameter, slicing starts from the very first element. If you do not specify the end, the slicing ends at the very last element.

Calling iterable[:] returns a slice that represents the whole iterable. In other words, it returns a copy of a list when called on a list.

Notice that this also creates a shallow copy.

For instance:

numbers = [1, 2, 3] new_numbers = numbers[:] print(numbers) print(new_numbers)

Output:

[1, 2, 3] [1, 2, 3] 3. The list() Function

To convert an object to a list in Python, you can use the built-in list() function. This function creates a new list object for the input argument.

When you call the list() function on a list in Python, you force it to create a copy of the original list. The type of this copy is also shallow.

For instance:

numbers = [1, 2, 3] new_numbers = list(numbers) print(numbers) print(new_numbers)

Output:

[1, 2, 3] [1, 2, 3] 4. The copy.copy() Function

As discussed earlier in this guide, there is a dedicated module copy for copying Python objects.

One of the functions in this module is the copy() function. This function creates a shallow copy of a Python object. You can use copy.copy() to create a copy of a list.

For instance:

import copy numbers = [1, 2, 3] new_numbers = copy.copy(numbers) print(numbers) print(new_numbers)

Output:

[1, 2, 3] [1, 2, 3] 5. The copy.deepcopy() Function

The only way to create a truly independent deep copy of a Python object is by using the copy.deepcopy() function.

The difference between a shallow copy and a deep copy is only relevant to objects that consist of objects. This is comprehensively explained earlier in this guide.

You can use copy.deepcopy() to create a deep copy of a list.

For example:

import copy numbers = [1, 2, 3] new_numbers = copy.deepcopy(numbers) print(numbers) print(new_numbers)

Output:

[1, 2, 3] [1, 2, 3] Conclusion

Today you learned how to copy a Python list successfully.

To recap, copying using the assignment operator is not possible. Instead of copying, it creates a new alias to the original object. This means changing the original object changes the “copy” as well.

To truly copy an object in Python, use the copy module’s functions:

copy.copy() for a shallow copy where compound objects are bound to the original object.

copy.deepcopy() for a deep and completely independent copy.

Further Reading

Python Interview Questions

Best Websites to Learn Python

Python How To Split A List To N Chunks Of Even Size

To split a list into N chunks in Python, you can use iterators:

def split_list(lst, chunk_size): for i in range(0, len(lst), chunk_size): yield lst[i:i+chunk_size] # Example use lst = [1, 2, 3, 4, 5, 6, 7, 8, 9] chunk_size = 3 for chunk in split_list(lst, chunk_size): print(chunk)

Notice that it isn’t always possible to produce a list where the chunks are equal in length. This is because the list elements might not be evenly divisible to the N chunks.

This is a comprehensive guide to splitting a list into N chunks in Python. In case you’re looking for a quick solution, I’m sure the above will do. But if you’re learning Python, make sure to read the entire guide to figure out multiple approaches to splitting lists into chunks. The idea of this guide is not to use an existing solution but to implement the logic yourself.

Let’s jump into it!

1. Iterator Approach

Here is an example of how to split a Python list into equally-sized chunks using iterators and the yield keyword:

def split_list(lst, chunk_size): for i in range(0, len(lst), chunk_size): yield lst[i:i+chunk_size] # Example usage lst = [1, 2, 3, 4, 5, 6, 7, 8, 9] chunk_size = 3 for chunk in split_list(lst, chunk_size): print(chunk)

This code will output the following:

[1, 2, 3] [4, 5, 6] [7, 8, 9]

Let’s take a closer look at the code.

The split_list function takes two arguments: a list lst and an integer chunk_size that specifies the size of each chunk. The function uses a for loop to iterate over the list, and yields a sublist of lst starting at the current index and ending at the current index plus the chunk size.

Here’s a step-by-step explanation of how the code works:

The split_list function is called with a list lst and a chunk size chunk_size.

The for loop iterates over the list, starting at index 0 and incrementing the index by chunk_size each time. For example, if the chunk size is 3, the first iteration will start at index 0, the second iteration will start at index 3, the third at 6, and so on.

On each iteration, the yield keyword constructs a sublist of lst starting at the current index and ending at the current index plus the chunk size. E.g. if the current index is 0 and the chunk size is 3, the sublist will be lst[0:3], which is the three elements of lst.

The for loop in the example code then iterates over the chunks yielded by split_list and prints each chunk.

The result of all of this is a list that is split into N chunks.

But What On Earth Does the ‘yield’ Do?

The previously introduced approach is the easiest one to split a list into chunks. But if you’re new to the yield keyword and iterators, this solution might just leave you confused.

In case you’re interested in iterators, make sure to read this complete guide to iterators and iterables in Python.

The next section teaches you how to do the previous approach without using the yield keyword but using lists instead.

2. For Loop Approach

Here’s another way to split lists into chunks in Python. This approach uses for loops and lists and is thus a bit more beginner-friendly than the previous example.

Here are the steps you need to take:

Determine the number of chunks you want to split the list into. Let’s call this number n.

Floor-divide the length of the list by n to find the size of each chunk (floor division rounds down so that the chunk size isn’t e.g. 3.3333 but instead just 3). We’ll call this number chunk_size.

Use the range() function to create a list of numbers that specify the indexes where each chunk should start. If the original list has 10 elements and you want to split it into 3 chunks, the list of starting indexes would be [0, 3, 6].

Use a for loop to iterate over the list of starting indexes, and use the list[start:end] syntax to extract each chunk from the original list.

# Set the number of chunks N = 4 # Create a list of numbers my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # Create an empty list to store the chunks chunks = [] # Iterate over the elements of the list in groups of N for i in range(0, len(my_list), N): # Extract each group of N elements as a sublist chunk = my_list[i:i + N] # Append the sublist to the list of chunks chunks.append(chunk) # Print the chunks print(chunks)

This code produces the following output:

[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10]] 2.1. List Comprehensions Can Do the Same

When you’re dealing with for loops, in some cases, you might be able to use a list comprehension to tidy up the code.

It’s always up for debate as to whether you should use comprehension or not. This is because a list comprehension for sure shortens the code but might actually make it less readable.

Anyway, here’s the previous example that uses a list comprehension instead of a for loop:

# Define the input list input_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # Define the chunk size chunk_size = 3 # Create the output list using a list comprehension output_list = [input_list[i:i + chunk_size] for i in range(0, len(input_list), chunk_size)] # Print the output list print(output_list)

This will produce the following output:

[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]] 3. While Loop

In the previous examples, you used a for loop to split a list into chunks. Because you can use a for loop, you can certainly do one with a while loop too!

Here’s what the code looks like when using a while loop:

# define the list of elements my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # define the size of the chunks chunk_size = 3 # create an empty list to store the chunks chunks = [] # create an index variable to track the current position in the list index = 0 # loop until the index is larger than the length of the list while index < len(my_list): # get the sublist of the current chunk chunk = my_list[index:index+chunk_size] # append the chunk to the list of chunks chunks.append(chunk) # update the index to the next chunk index += chunk_size # print the resulting chunks print(chunks)

Output:

[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]

This solution creates an empty list to store the resulting chunks and then uses a while loop to iterate over the original list by chunk sizes, appending each chunk to the list of chunks. The index variable is used to track the current position in the list and is updated at each iteration to the next chunk.

4. Use NumPy to Split into N Chunks

In Python, there’s a popular math and science module called NumPy that the scientific Python community uses a lot.

If you’re using NumPy already, you can use the array_split() function to split lists into chunks. This function takes in the list as the first argument and the size of the chunks as the second argument.

Notice that this approach forces the elements into N chunks. The leftover values aren’t placed in their own chunk but are pushed to the last chunk instead.

For example, let’s split a list of 10 numbers into 3 chunks:

import numpy as np # Define the list to be split my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] # Split the list into chunks of size 3 chunks = np.array_split(my_list, 3) # Print the resulting chunks print(chunks) # Output: [array([1, 2, 3]), array([4, 5, 6]), array([7, 8, 9, 10])]

Notice how the last chunk now has 4 elements instead of taking the extra element and placing it in its own chunk.

Thanks for reading. Happy coding!

Read Also

Comprehensions in Python

Python How To Convert Bytes To String (5 Approaches)

To convert bytes into a string in Python, use the bytes.decode() method.

For instance:

name_byte = b'Alice' name_str = name_byte.decode() print(name_str)

Output:

Alice

This is the quick answer.

However, depending on the context and your needs, there are other ways to convert bytes to strings.

In this guide, you learn how to convert bytes to string in 5 different ways in different situations.

Here’s a short review of the byte-to-string converting methods:

Method Example

1. The decode() method of a byte string byte_string.decode(‘UTF-8’)

2. The built-in str() method str(byte_string, ‘UTF-8’)

3. Codecs decode() function codecs.decode(byte_string)

4. Pandas dataframe decode() method df[‘column’].str.decode(“utf-8”)

5. The join() method with map() function “”.join(map(chr, byte_str))

Let’s jump to it!

Bytes vs Strings in Python

There is a chance you are looking to convert bytes to strings because you do not know what they are. Before jumping into the conversions, let’s take a quick look at what are bytes in the first place.

Why Bytes?

A computer doesn’t understand the notion of “text” or “number” as is. This is because computers operate on bits, that is, 0s and 1s.

Storing data to a computer happens by using groups of bits, also known as bytes. Usually, there are 8 bits in a byte. But this might vary depending on what system you’re using.

Byte Strings in Python

In Python, a byte string is a sequence of bytes that the computer understands but humans can’t.

A string is a sequence of characters and is something we humans can understand but cannot directly store in a computer.

This is why any string needs to be converted to a byte string before the computer can use it.

In Python, a bytes object is a byte representation of a string. A bytes object is prefixed with the letter ‘b‘.

For example, take a look at these two variables:

name1 = 'Alice' name2 = b'Alice'

Here:

name1 is a str object.

name2 is a bytes object.

You can verify this by printing out the data types of these variables:

name1 = 'Alice' name2 = b'Alice' print(type(name1)) print(type(name2))

Output:

As I mentioned earlier, the byte string is something that is hard to understand. In the above code, this isn’t clear as you can just read the b’Alice’ very clearly.

Byte String vs String in Python

To see the main difference between the byte string and a string, let’s print the words character by character.

First, let’s do the name1 variable:

name1 = 'Alice' name2 = b'Alice' for c in name1: print(c)

Output:

A l i c e

Now, let’s print each byte in the name2 bytes object:

name1 = 'Alice' name2 = b'Alice' for c in name2: print(c)

Output:

65 108 105 99 101

Here you can see there is no way for you to tell what those numbers mean. Those numbers are the byte values of the characters in a string. Something that a computer can understand.

To make one more thing clear, let’s see what happens if we print the bytes object name2 as-is:

name1 = 'Alice' name2 = b'Alice' print(name2)

Output:

b'Alice'

As your surprize, it clearly says “Alice”. This isn’t too hard to read, is it?

The reason why the byte string prints out as a readable string is because what you see is actually a string representation of the bytes object.

Python does this for the developer’s convenience.

If there was no special string representation for a bytes object, printing bytes would be nonsense.

Anyway, now you understand what is a bytes object in Python, and how it differs from the str object.

Now, let’s see how to convert between bytes and string.

1. The decode() Function

Given a bytes object, you can use the built-in decode() method to convert the byte to a string.

You can also pass the encoding type to this function as an argument.

For example, let’s use the UTF-8 encoding for converting bytes to a string:

byte_string = b"Do you want a slice of xf0x9fx8dx95?" string = byte_string.decode('UTF-8') print(string)

Output:

Do you want a slice of 🍕? 2. The str() Function

Another approach to convert bytes to string is by using the built-in str() function.

This method does the exact same thing as the decode() method in the previous example.

For instance:

byte_string = b"Do you want a slice of xf0x9fx8dx95?" string = str(byte_string, 'UTF-8') print(string)

Output:

Do you want a slice of 🍕?

Perhaps the only downside to this approach is in the code readability.

If you compare these two lines:

name_str = str(byte_string, 'UTF-8') name_str = byte_string.decode('UTF-8')

You can see the latter is more explicit about decoding the bytes to a string.

3. Codecs decode() Function

Python also has a built-in codecs module for text decoding and encoding.

This module also has its own decode() function. You can use this function to convert bytes to strings (and vice versa).

For instance:

import codecs byte_string = b"Do you want a slice of xf0x9fx8dx95?" name_byte = codecs.decode(byte_string) print(name_byte)

Output:

Do you want a slice of 🍕? 4. Pandas decode() Function

If you are working with pandas and you have a data frame that consists of bytes, you can easily convert them to strings by calling the str.decode() function on a column.

For instance:

import pandas as pd data_bytes = {'column' : [b'Alice', b'Bob', b'Charlie']} df = pd.DataFrame(data=data_bytes) data_strings = df['column'].str.decode("utf-8") print(data_strings)

Output:

0 Alice 1 Bob 2 Charlie Name: column, dtype: object 5. map() Function: Convert a Byte List to String

In Python, a string is a group of characters.

Each Python character is associated with a Unicode value, which is an integer.

Thus, you can convert an integer to a character in Python.

To do this, you can call the built-in chr() function on an integer.

Given a list of integers, you can use the map() function to map each integer to a character.

Here is how it looks in code:

byte_data = [65, 108, 105, 99, 101] strings = "".join(map(chr, byte_data)) print(strings)

Output:

Alice

This piece of code:

Converts the integers to corresponding characters.

Returns a list of characters.

Merges the list of characters to a single string.

To learn more about the map() function in Python, feel free to read this article.

Be Careful with the Encoding

There are dozens of byte-to-string encodings out there.

In this guide, we only used the UTF-8 encoding, which is the most popular encoding type.

The UTF-8 is also the default encoding type in Python. However, UTF-8 encoding is not always the correct one.

For instance:

s = b"test xe7xf8xe9" s.decode('UTF-8')

Output:

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe7 in position 5: invalid continuation byte

This error means there is no character in the UTF-8 encoding that corresponds to the bytes in the string.

In other words, you should be using a different encoding.

You can use a module like chardet to detect the character encodings. (Notice that this module is not maintained, but most of the info you learn about it is still applicable.)

However, no approach is 100% foolproof. This module gives you its best guess about the encoding and the probability associated with it.

Anyway, let’s say the above byte string can be decoded using the latin1 encoding as well as the iso_8559_5 encoding.

Now let’s make the conversion:

s = b"test xe7xf8xe9" print(s.decode('latin1')) print(s.decode('iso8859_5'))

Output:

test çøé test чјщ

This time there is no error. Instead, it works with both encodings and produces a different result.

So be careful with the encodings!

If you see an error when doing a conversion, the first thing you need to do is to figure out the encoding used. Then you should use that particular encoding to encode/decode your values to get it right.

Conclusion

Today you learned how to convert bytes to strings in Python.

To recap, there is a bunch of ways to convert bytes to strings in Python.

To convert a byte string to a string, use the bytes.decode() method.

If you have a list of bytes, call chr() function on each byte using the map() function (or a for loop)

If you have a pandas dataframe with bytes, call the .str.decode() method on the column with bytes.

By default, the Python character encoding is usually UTF-8.

However, this is not always applicable. Trying to encode a non-UTF-8 byte with UTF-8 produces an error. In this situation, you should determine the right character encoding before encoding/decoding. You can use a module like chardet to do this.

Further Reading

Python Interview Questions

Update the detailed information about Python Filter List: 5 Practical Methods Explained on the Hatcungthantuong.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!