Trending March 2024 # How To Copy A File In Python: Shutil.copy() # Suggested April 2024 # Top 5 Popular

You are reading the article How To Copy A File In Python: Shutil.copy() 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 How To Copy A File In Python: Shutil.copy()

To copy a file in Python, use the shutil module’s copyfile() function.

from shutil import copyfile copyfile(src, dst)

This piece of code copies the contents of the file src to a destination file dst.

Both src and dst are strings that represent the names of the files (or rather, paths to the files).

If a file dst does not exist, a new file is created. If it does, the file behind that path is overridden.

Real-Life Example of How to Copy a File with Python

To understand how to copy a file in Python, you need to see an example.

Let’s start with a setup like this, where you have two files chúng tôi and chúng tôi in the same folder on your computer:

The chúng tôi is a program that copies the file called chúng tôi to a new file called chúng tôi .

Because you are working in the same folder, you do not need to worry about the full paths of the files.

All you need is the names of the files.

Here is the code:

from shutil import copyfile src = 'Example1.txt' dst = 'Example2.txt' copyfile(src, dst)

After running this piece of code, you should see a file called chúng tôi appear in the same folder:

This is how easy it is to copy a file using Python.

How to Copy a File to Different Folder

More often than not your files are not going to be in the same folder.

If this is the case, accessing the files with their names is not possible.

If you want to copy a file from one folder to another, you need to specify the path of the source and the destination.

The path obviously depends entirely on your file setup.

Let’s see a simple and demonstrative example.

Let’s say you have a folder called Project. Inside this folder, you have three subfolders called Scripts, Images, and Favorites.

Inside the Scripts folder, you have a file called chúng tôi . This is a program file that copies an image called chúng tôi from Images to Favorites.

Here is an illustration of the folder structure:

Because the code file, the source file, and the destination all lie in different folders, you need to work with paths instead of file names.

Here is how the chúng tôi file looks in the Scripts folder

from shutil import copyfile src = '../Images/zebra.png' dst = '../Favorites/zebra.png' copyfile(src, dst)

Let’s examine the paths to understand how they work.

The ../ refers to the parent folder. In the case of the Scripts folder, this refers to the Project folder in which the Images and Favorites folders live.

The parent folder has to be specified because otherwise, the chúng tôi script would search inside the Scripts folder which has no other files except for the code file.

Inside the Project folder, accessing the subfolders Images and Favorites is trivial:

Images/zebra.png is the path to the image.

Favorites/zebra.png is the desired path to the copied file.

Conclusion

Today you learned how to copy a file in Python using the shutil module’s copyfile() function:

from shutil import copyfile copyfile(src, dst)

Where:

src is the path of the file to be copied as a string.

dst is the destination file’s path as a string.

In case the source file and the destination file lie in the same folder, it is enough to use the names of the files.

Thanks for reading. Happy coding!

Further Reading

How to Read a Text File in Python

Python Interview Questions

You're reading How To Copy A File In Python: Shutil.copy()

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

How To Copy Files From One Folder To Another Using Python?

A file is a collection of information or data that is stored on a computer. You are already familiar with several file types, such as your audio, video, and text files.

Text files and binary files are the two categories into which we often split files. Simple text is contained in text files, as opposed to binary data, which can only be read by computers.

A group of files and subdirectories is called a directory or folder. A subdirectory is a directory present inside a directory. Numerous operating system functions can be carried out automatically.

File Operations Using Python

Python provides various methods to perform operations on the files and folders of the underlying operating system.

The OS module in Python has functions for adding and deleting folders, retrieving their contents, changing the directory, locating the current directory, and more. Import this module, we will use the listdir() method of it to fetch the files.

Similarly, the shutil module provides a number of functions for dealing with operations on files and associated collections. It gives users the option to copy and delete files. You can copy the contents of one folder to another using the shutil.copy(), shutil.copy2() and shutil.copytree() methods of this module.

You can include these functions in your file by importing their respective modules as shown below −

import

shutil shutil

.

submodule_name

(

arguments passed

)

Using shutil.copy() operation

Using this function, the text or content of the source file is copied to the target file or directories. Additionally, the permission mode of the file is preserved, but the file metadata (such as the “Date Creation”, “Date Modification” etc..) is not preserved.

Syntax

Following is the syntax of the shutil.copy() method−

shutil.copy(origin, target)

where

Origin − A string containing the source file’s location or path

Target − A string containing the destination file’s location or path.

Example

Following is an example of to copy files from one folder to other using shutil.copy() operation −

import

os

import

shutil files

=

os

.

listdir

(

origin

)

for

file_name

in

files

:

shutil

.

copy

(

origin

+

file_name

,

target

+

file_name

)

print

(

"Files are copied successfully"

)

Output

Following is an output of the above query:

Files are copied successfully

Note − Both a relative and an absolute path can be used to copy a file. The file’s location on the disc is indicated by the path

Here we are providing the folder path of both the source and the destination of the files.

Using shutil.copy2() operation

First of all, this function is exactly like copy() with the exception that it keeps track of the source file’s metadata.

The execution program for this is exact same as shutil.copy(). The only difference is that while fetching the file to directory, in place of shutil.copy() we write shutil.copy2().

shutil

.

copy2

(

origin

+

file_name

,

target

+

file_name

)

Syntax

Following is the syntax of the shutil.copy2() method –

shutil.copy2(origin, target)

Origin and target values are same as defined above.

The copy2() function in this code does one additional operation in addition to a copy() which is to keep the metadata.

Using shutil.copytree() method

This function moves a file and any subdirectories it contains from one directory to another.

This indicates that both the source and the destination include the file. The string must contain the names of both parameters.

Syntax

Following is the syntax of the shutil.copytree() method –

shutil.copytree(origin, target)

Origin and target values are same as defined above.

Example

Following is an example of to copy files from one folder to other using shutil.copytree() operation:

import

shutil

print

(

"File Copied Successfully"

)

Output

Following is an output of the above query:

File Copied Successfully

As an output we will be able to see the changes made after the execution i.e. the ‘Works’ folder gets copied to the ‘Works TP’ folder with the name of ‘newfolder’ as assigned in the code above containing all the files inside it which was there in the Works folder.

In order to obtain a duplicate of that file, we have included the copytree() function in this code.

How To Flatten A Matrix Using Numpy In Python?

In this article, we will show you how to flatten a matrix using the NumPy library in python.

numpy.ndarray.flatten() function

The numpy module includes a function called numpy.ndarray.flatten() that returns a one-dimensional copy of the array rather than a two-dimensional or multi-dimensional array.

In simple words, we can say that it flattens a matrix to 1-Dimension.

Syntax ndarray.flatten(order='C') Parameters

order − ‘C’, ‘F’, ‘A’, ‘K’ (optional)

When we set the order parameter to ‘C,’ the array is flattened in row-major order.

When the ‘F’ is set, the array is flattened in column-major order.

Only when ‘a’ is Fortran contiguous in memory and the order parameter is set to ‘A’ is the array flattened in column-major order. The final order is ‘K,’ which flattens the array in the same order that the elements appeared in memory. This parameter is set to ‘C’ by default.

Return Value − Returns a flattened 1-D matrix

Method 1 − Flattening 2×2 Numpy Matrix of np.array() type Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

Use the import keyword, to import the numpy module with an alias name(np).

Use the numpy.array() function(returns a ndarray. The ndarray is an array object that satisfies the given requirements), for creating a numpy array by passing the 2-Dimensional array(2rows, 2columns) as an argument to it.

Print the given input 2-Dimensional matrix.

Apply flatten() function (flattens a matrix to 1-Dimension) of the numpy module on the input matrix to flatten the input 2D matrix to a one-dimensional matrix.

Print the resultant flattened matrix of an input matrix.

Example

The following program flattens the given input 2-Dimensional matrix to a 1-Dimensional matrix using the flatten()function and returns it −

import

numpy

as

np

inputMatrix

=

np

.

array

(

[

[

3

,

5

]

,

[

4

,

8

]

]

)

print

(

“The input numpy matrix:”

)

print

(

inputMatrix

)

flattenMatrix

=

inputMatrix

.

flatten

(

)

print

(

“Resultant flattened matrix:”

)

print

(

flattenMatrix

)

Output

On executing, the above program will generate the following output −

The input numpy matrix: [[3 5] [4 8]] Resultant flattened matrix: [3 5 4 8] Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

Use the numpy.array() function(returns a ndarray. The ndarray is an array object that satisfies the given requirements), for creating a numpy array by passing the 4-Dimensional array(4rows, 4columns) as an argument to it.

Print the given input 4-Dimensional matrix.

Calculate the number of elements of the matrix by multiplying the length of the NumPy array with itself. Here these values give the number of columns required.

Use the reshape() function(reshapes an array without affecting its data) to reshape the array and flatten the input matrix(4D) to a one-dimensional matrix.

Print the resultant flattened matrix of an input matrix.

Example

The following program flattens the given input 4-Dimensional matrix to a 1-Dimensional matrix using reshape() function and returns it −

import

numpy

as

np

inputMatrix

=

np

.

array

(

[

[

1

,

2

,

3

,

97

]

,

[

4

,

5

,

6

,

98

]

,

[

7

,

8

,

9

,

99

]

,

[

10

,

11

,

12

,

100

]

]

)

matrixSize

=

len

(

inputMatrix

)

*

len

(

inputMatrix

)

print

(

“The input numpy matrix:”

)

print

(

inputMatrix

)

flattenMatrix

=

np

.

reshape

(

inputMatrix

,

(

1

,

matrixSize

)

)

print

(

“Resultant flattened matrix:”

)

print

(

flattenMatrix

)

Output

On executing, the above program will generate the following output −

The input numpy matrix: [[ 1 2 3 97] [ 4 5 6 98] [ 7 8 9 99] [ 10 11 12 100]] Resultant flattened matrix: [[ 1 2 3 97 4 5 6 98 7 8 9 99 10 11 12 100]] Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

Use the numpy.matrix() function(returns a matrix from a string of data or an array-like object. The resulting matrix is a specialized 4D array), for creating a numpy matrix by passing the 4-Dimensional array(4 rows, 4 columns) as an argument to it.

Print the resultant flattened matrix of an input matrix.

Example

The following program flattens the given input 4-Dimensional matrix to a 1-Dimensional matrix using the flatten()function and returns it −

import

numpy

as

np

inputMatrix

=

np

.

matrix

(

‘[11, 1, 8, 2; 11, 3, 9 ,1; 1, 2, 3, 4; 9, 8, 7, 6]’

)

print

(

“The input numpy matrix:”

)

print

(

inputMatrix

)

flattenMatrix

=

inputMatrix

.

flatten

(

)

print

(

“Resultant flattened matrix:”

)

print

(

flattenMatrix

)

Output

On executing, the above program will generate the following output −

The input numpy matrix: [[11 1 8 2] [11 3 9 1] [ 1 2 3 4] [ 9 8 7 6]] Resultant flattened matrix: [[11 1 8 2 11 3 9 1 1 2 3 4 9 8 7 6]] Conclusion

In this article, we learned how to flatten the matrix in python using three different examples. We learned how to take a matrix in Numpy using two different methods: numpy.array() and NumPy.matrix(). We also learned how to flatten a matrix using the reshape function.

How To Create A Dictionary Of Sets In Python?

In this article, we will learn how to create a dictionary of sets in Python.

Methods Used

The following are the various methods used to accomplish this task −

Using the Naive Method

Using defaultdict() Method

Using setdefault() Method

Method 1: Using the Naive Method

In this method, we create a dictionary of sets by passing sets as values to the keys.

Syntax { ‘Key’: Set 1, ‘Key’:Set 2,…………..,’Key’: Set n} Algorithm (Steps)

Following are the Algorithms/steps to be followed to perform the desired task. −

Create a variable to store the dictionary containing the values as a set without duplicate elements (i.e, dictionary of sets).

Print the input dictionary of sets.

Example

The following program creates a dictionary of sets(without duplicates) in python using the Naive Method −

# creating a dictionary containing the values as set # without duplicate elements # (dictionary of sets) inputDict = {'Employ ID': {10, 11, 12, 14, 15}, 'Employ Age': {25, 30, 40, 35, 28}} # printing the input dictionary of sets print(inputDict) Output

On execution, the above program will generate the following output −

{'Employ ID': {10, 11, 12, 14, 15}, 'Employ Age': {35, 40, 25, 28, 30}} Creating a Set having Duplicates

In general, the set in python does not allow duplicates i,e which removes all the repeated elements.

Example

The below example shows that a python set will not allow duplicates.

The following program creates a dictionary of sets(containing duplicates) in python using the Naive Method −

# creating a dictionary containing the values as set # with duplicates elements # the set does not allow duplicates inputDict = {'Employ ID': {10, 11, 12, 13, 13, 14, 15, 10, 12, 11}, 'Employ Age': {25, 30, 30, 40, 25, 35, 40, 28, 33, 25}} # printing the input dictionary print(inputDict) Output

On executing, the above program will generate the following output −

{'Employ ID': {10, 11, 12, 13, 14, 15}, 'Employ Age': {33, 35, 40, 25, 28, 30}}

In the above example, we can observe that all the duplicates are removed, and printed only the unique elements. Hence proved that a python set removes does not allow duplicates.

Method 2: Using defaultdict() Method

In this method, the default set will be created and the key-values pairs will be passed to it.

Syntax defaultdict(set)

Passing the dictionary with key and value −

Here,

dictionary − input dictionary

key − key of a dictionary

value − the value of a dictionary passed as set.

Algorithm (Steps)

Following are the Algorithms/steps to be followed to perform the desired task. −

Use the import keyword to import the defaultdict from the collections module.

Use the defaultdict() method, to create an empty set of dictionary using the by-passing set as an argument to it.

Give the key-value pairs for the dictionary using the [] operator.

Print the created dictionary of sets.

Example

The following program creates a dictionary of sets in python using the defaultdict() function −

# importing defaultdict from the collections module from collections import defaultdict # creating an empty set of the dictionary using the # defaultdict() method by passing set as argument to it dictionary = defaultdict(set) # giving the first key-value pair of a dictionary # giving the second key-value pair of a dictionary # printing the created dictionary of sets print(dictionary) Output

On execution, the above program will generate the following output −

Method 3: Using setdefault() Method

The value of a key within the dictionary is returned by the setdefault() method. If not, a key and value are inserted into the dictionary.

Syntax dict.setdefault(key, default_value)

here,

key − It is the key that must be searched in a dictionary.

default_value − It is the value of a specific key

Algorithm (Steps)

Following are the Algorithms/steps to be followed to perform the desired task. −

Create a variable to store the input dictionary containing the value as a set without duplicate elements (i.e, dictionary of sets).

Use the setdefault() function, to access the values of ‘Employ ID’ as a set of the input dictionary by passing ‘Employ ID’ as an argument to it.

Use the setdefault() function, to access the values of ‘Employ Age’ as a set of the input dictionary by passing ‘Employ Age’ as an argument to it.

Create a new key-value pair with values as set using the setdefault() method.

Print the resultant dictionary after adding 3rd set.

Example

The following program creates a dictionary of sets and accesses its elements of it using the setdefault() method −

# creating a dictionary containing the values as sets # (dictionary of sets) inputDict = {'Employ ID': {10, 11, 12, 14, 15, 11}, 'Employ Age': {25, 30, 40, 35, 28, 28}} # accessing the values of 'Employ ID' of the dictionary as a set # using setdefault() function print("Accessing Employ ID:", inputDict.setdefault('Employ ID')) # accessing the values of 'Employ Age' of dictionary as a set # using setdefault() function print("Accessing Employ Age:", inputDict.setdefault('Employ Age')) # set the third set of values for the dictionary using setdefault method # Employee names inputDict = inputDict.setdefault( 'Employ Name', {'rohit', 'virat', 'pandya', 'smith', 'warner'}) # printing the dictionary after adding 3rd set print(inputDict) Output

On execution, the above program will generate the following output −

Accessing Employ ID: {10, 11, 12, 14, 15} Accessing Employ Age: {35, 40, 25, 28, 30} {'pandya', 'rohit', 'smith', 'virat', 'warner'} Conclusion

In this article, we learned how to use 3 different methods to create a dictionary of sets in Python. This is necessary to remove duplicate data for certain keys, such as only keeping unique roll numbers, unique job ids, etc. We also learned how to use the setdefault() function to retrieve the dictionary of sets.

How To Use Pprint In Python?

Introduction

Improve your Python output with the power of pprint! Properly formatted and visually appealing output can greatly enhance your code debugging experience. This article introduces pprint, a Python library that serves as a Data Pretty Printer. Whether you’re dealing with JSON files or working with dictionaries, pprint can help you handle large amounts of data more effectively. Say goodbye to messy outputs and confusing structures. Best of all, pprint is an inbuilt Python library, so no separate installation is required. Let’s dive in and make your outputs shine!

This article was published as a part of the Data Science Blogathon.

What is pprint()?

It stands for “Pretty Print”, and is one of the native Python libraries that allows you to customize your outputs with its numerous parameters and flags for its single class pprint(). Here is the official documentation to list all of its properties and usage.

pprint() Parameters

The library contains just a single class, called pprint(). There are in total six parameters that can be used with this class. Here is a short description of the parameters along with their default values:

indent: The number of spaces to indent each line, this value can help when specific formatting is needed. Default value = 1

width: Maximum characters that can be in a single line. If the number of words exceeds this limit, the remaining text will be wrapped on the lines below. Default value = 80

depth: The number of depth levels to be shown while using nested data types. By default, it shows all the data, but if specified, the data beyond the depth level is shown as a series of dots ( . . . ). Default value = None

stream: This is used to specify an output stream and is mainly used to pretty print a file. Its default behavior is to use sys.stdout. Default value = None

compact: This is a boolean argument. If set to True, it will consolidate complex data structures into single lines, within the specified width. If the value is the default (ie. False) all the items will be formatted on separate lines. Default value = False

sort_dicts: This is also a boolean argument. While printing dictionaries with pprint(), it prints the key-value pair sorted according to the key name alphabetically. When set to false, the key, value pairs will be displayed according to their order of insertion. Default value = True

Now enough with the technical stuff, let’s jump into the programming part!

Basics of pprint()

First, we import the pprint module at the beginning of our notebook.

import pprint

Now you can either use the pprint() method or instantiate your pprint object with PrettyPrinter().



Now let us create a sample dictionary to demonstrate some of the arguments of the class pprint().

sample_dict = { 'name': 'Sion', 'age': 21, 'message': 'Thank you for reading this article!', 'topic':'Python Libraries' }

If we simply print out this dictionary using print, what we get is:

{'name': 'Sion', 'age': 21, 'message': 'Thank you for reading this article!', 'topic': 'Python Libraries'}

Now that doesn’t look much appealing, does it? But still one might argue that this output format is okay since you can clearly see which value belongs to which key, but what happens if these values are extremely long, and nested. Or if the volume of our key-value pairs is much much more? That’s when it all goes downhill. It will become very very difficult to read, but worry not, print to the rescue:

pprint.pprint(sample_dict)

Firstly, all the pairs have their separate row, which increases the readability tenfold. Also if you look closely, all the elements are automatically sorted according to the keys.

The pprintpp Module

Also Read: How to Read Common File Formats in Python – CSV, Excel, JSON, and more!

Text Wrapping

Image Source: Elle

Most people might know the basics that I showed above. Now let’s use some of the other parameters to further customize our outputs.

Another basic usage is text wrapping. Suppose you are not satisfied by just printing the key-value pairs on separate lines, but want to have the text wrapped when the length of the line exceeds a certain amount. For this, we can use the width parameter.

pprint.pprint(sample_dict, width = 30)

Apart from this, we can use the indent parameter to add indentation in front of each row for better readability.

pprint.pprint(sample_dict, width = 30, indent = 10)

Here is an example for the usage of compact and width parameters:

import pprint stuff = ['spam', 'eggs', 'lumberjack', 'knights', 'ni'] stuff.insert(0, stuff[:]) pp = pprint.PrettyPrinter(indent=4) pp.pprint(stuff) pp = pprint.PrettyPrinter(width=41, compact=True) pp.pprint(stuff) Deep Nested Objects

Image Source: Missouri Dept. of Conservation

Sometimes while working with highly nested objects, we just want to view just the outer values and are not interested in the deeper levels. For example, if we have a nested tuple like this:

sample_tuple = ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', ('parrot', ('fresh fruit',))))))))

Now if we use print or print, the outputs will be almost similar:

print(sample_tuple) > ('spam', ('eggs', ('lumberjack', ('knights', ('ni', ('dead', ('parrot', ('fresh fruit',)))))))) pp.pprint(sample_tuple)

However, if the depth parameter is specified, anything deeper than that will be truncated:

pprint.pprint(sample_tuple, depth=2) pprint.pprint(sample_tuple, depth=1) p = pprint.PrettyPrinter(depth=6) p.pprint(sample_tuple) pprint() vs PrettyPrinter()

The difference between these two is that the pprint() method uses the default arguments and settings of the libraries, which you can change like we previously saw, but these changes are temporary.

With PrettyPrinter(), you can create a class, with your own specifications and override the default settings to create permanent class objects which retain their forms and values all over your project.

import pprint coordinates = [ { "name": "Location 1", "gps": (29.008966, 111.573724) }, { "name": "Location 2", "gps": (40.1632626, 44.2935926) }, { "name": "Location 3", "gps": (29.476705, 121.869339) } ] pprint.pprint(coordinates, depth=1) > [{...}, {...}, {...}] pprint.pprint(coordinates)

As you can see, the arguments supplied were just temporary. Conversely, these settings are stored with PrettyPrinter(), and you can use them wherever in the code you want, without any change in functionality:

import pprint my_printer = pprint.PrettyPrinter(depth=1) coordinates = [ { "name": "Location 1", "gps": (29.008966, 111.573724) }, { "name": "Location 2", "gps": (40.1632626, 44.2935926) }, { "name": "Location 3", "gps": (29.476705, 121.869339) } ] my_printer.pprint(coordinates) > [{...}, {...}, {...}] Conclusion Frequently Asked Questions

Q1. What is Pprint used for?

A. Pprint (pretty print) is a Python module used for formatting complex data structures more readably and organized, especially when printing them to the console or writing to a file.

Q2. What is the difference between print and Pprint?

A. The main difference between print and Pprint is that Pprint is designed to format complex data structures such as dictionaries and lists, preserving their structure and providing indentation. In contrast, print is used for simple output of values or strings.

Q3. Is Pprint standard in Python?

A. Pprint is not a standard built-in module in Python, but it is included in the Python Standard Library, meaning it is available by default in most Python installations.

Q4. Is Pprint native to Python?

A. Yes, Pprint is native to Python as it is included in the Python Standard Library, allowing developers to utilize its functionality without needing external dependencies or installations.

References

The media shown in this article are not owned by Analytics Vidhya and is used at the Author’s discretion. 

Related

Update the detailed information about How To Copy A File In Python: Shutil.copy() 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!