Hi,
I am assigning the list/dict values to another list/dict variable as shown below.
List :
List1 = [1,4,7,9,10,12,33]
List2 = List1
Dict :
Dict1 = {1:[1,4,7,9,10,12,33],2;[6,8,9]}
Dict2 = Dict1
If we have more data in list (say index of list is '2000').Then the above assignment will not take much time
Is there any other way is there for assiging the list /dict?
Thanks
PSB
6 2968
Hi,
I am assigning the list/dict values to another list/dict variable as shown below.
List :
List1 = [1,4,7,9,10,12,33]
List2 = List1
Dict :
Dict1 = {1:[1,4,7,9,10,12,33],2;[6,8,9]}
Dict2 = Dict1
If we have more data in list (say index of list is '2000').Then the above assignment will not take much time
Is there any other way is there for assiging the list /dict?
Thanks
PSB
Use the copy module to make copies. Because of "pass by reference" the above method yeilds:
>>> aList = [1, 2, 3]
>>> bList = aList
>>> bList.append(4)
>>> aList
[1, 2, 3, 4]
Use the copy module to make copies. Because of "pass by reference" the above method yeilds:
>>> aList = [1, 2, 3]
>>> bList = aList
>>> bList.append(4)
>>> aList
[1, 2, 3, 4]
For dictionaries, you can also use dict.update(): -
>>> aDict = dict(a=1, b=2, c=3)
-
>>> bDict = {}
-
>>> bDict.update(aDict)
-
>>> bDict['d'] = 4
-
>>> aDict
-
{'a': 1, 'c': 3, 'b': 2}
-
>>> bDict
-
{'a': 1, 'c': 3, 'b': 2, 'd': 4}
-
>>>
Which is faster ,whether assigning the variables directlyor using the methods for assigning the variables?
-PSB
Which is faster ,whether assigning the variables directlyor using the methods for assigning the variables?
-PSB
See my timeit.Timer usage post in the Python > Code section.
Assigning the dictionary variable to another dictionary variable ,is taking less time than using the method "update()".
But I was thinking "update()" will take less time. - Sample
-
-
>>> t1 = timeit.Timer("aDict = dict(a=1, b=2, c=3);bDict =aDict")
-
>>> print t1.timeit()
-
1.89162365608
-
>>>
-
t2 = timeit.Timer("aDict = dict(a=1, b=2, c=3);bDict = {};bDict.update(aDict)")
-
>>> print t2.timeit()
-
3.35477972759
-
Assigning the dictionary variable to another dictionary variable ,is taking less time than using the method "update()".
But I was thinking "update()" will take less time. - Sample
-
-
>>> t1 = timeit.Timer("aDict = dict(a=1, b=2, c=3);bDict =aDict")
-
>>> print t1.timeit()
-
1.89162365608
-
>>>
-
t2 = timeit.Timer("aDict = dict(a=1, b=2, c=3);bDict = {};bDict.update(aDict)")
-
>>> print t2.timeit()
-
3.35477972759
-
The thing is this:
in your t1 test, you merely assign a reference (pointer to aDict) to the variable bDict. There is NO copy of dict a going on there.
>>> aDict = dict(a=1, b=2, c=3)
>>> aDict
{'a': 1, 'c': 3, 'b': 2}
>>> bDict = aDict
>>> bDict['d'] = 4
>>> aDict
{'a': 1, 'c': 3, 'b': 2, 'd': 4}
>>>
t2 is actually timing a copy of the first dict to the second.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Ricky W. Hunt |
last post by:
I came across something by accident; assigning one object to another (as in
object1 = object2) allows me to "work on" object2 just like it was object1.
For instance, in order to be able to...
|
by: carrajo |
last post by:
Hey,
I'm trying to duplicate the following:
Select List 1
---
Apple
Orange
Banana
|
by: matthew.macdonald-wallace |
last post by:
Hi all,
I'm trying to add functionality to an app written in C# so that a list
of people is displayed in a list box on the left hand side and then
selected individuals can be added to another...
|
by: Emin |
last post by:
Dear Experts,
How much slower is dict indexing vs. list indexing (or indexing into a
numpy array)? I realize that looking up a value in a dict should be
constant time, but does anyone have a...
|
by: subashini Thiyagarajan |
last post by:
Dear friends,
two list boxes are there.when i select any data from first list box second list box should automatically deactivated.how to do?
i have populated list box items from database in both...
|
by: troy_lee |
last post by:
I have a continuous form that has one combo box (cbo1) with the
selectable values of "Removal" and "Installation". I would like to
change another combo box (cbo2) value list based on the selection...
|
by: Tem |
last post by:
Thanks for all your responses. I see why c# is such a powerful language and
starting to like it more and more.
I also need help sorting this nested list. It is a little tricky. not sure
where to...
|
by: rmurgia |
last post by:
Is there a way to set up a dropdown list and have the description field be displayed as a selection for the user and also be displayed in the input field after the user selects it, but have the value...
|
by: munkee |
last post by:
Hi all,
Basically I want to use two list boxes side by side. One populated with a list of items and between them some buttons to allow the user to select and move items from the left hand list box...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |