Can assignment operator be used to copy elements in Python?

+1 vote
I have a dictionary in Python. I want to initialize them first and then copy elements from one to another. How to do it?
Jul 29 in Python by Arvind
• 2,960 points
19 views

1 answer to this question.

0 votes
Yes assignment operator can be used to copy elements from one dictionary to other. Also you can use copy() to do the same. Have a look at the following
>>> a,b = {},{} # initialize the dictionaries 
>>> a = {1:"hi",2:"hello"}
>>> a
{1: 'hi', 2: 'hello'}
>>> b=a # use of assignment operator
>>> b
{1: 'hi', 2: 'hello'}
>>> c=b.copy() # use of copy method
>>> c
{1: 'hi', 2: 'hello'}
>>> 

answered Jul 29 by Neel
• 3,010 points

edited Jul 30 by Neel

Related Questions In Python

0 votes
1 answer

Adding elements to a list in python

Use extend() instead: l = [5, 7, 12, ...READ MORE

answered Apr 23, 2018 in Python by Nietzsche's daemon
• 4,260 points
39 views
0 votes
1 answer

Which IDE can be used for Python on Mac OS X

Have tried many different (Kate, Eclipse, Scite, ...READ MORE

answered Oct 15, 2018 in Python by SDeb
• 13,160 points
61 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 226 views
0 votes
1 answer

How to add elements to a list in Python?

To initialize an empty list do this: new_list ...READ MORE

answered Jul 23 in Python by Neel
• 3,010 points
24 views
0 votes
1 answer

Is it possible to run a function in Python using the command line?

Suppose your file name is demo.py and ...READ MORE

answered Jun 26 in Python by Neel
• 3,010 points
47 views