473,385 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Best of way of assigning list/dict values to another list/dict variable

440 256MB
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
Apr 18 '07 #1
6 2968
bartonc
6,596 Expert 4TB
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]
Apr 19 '07 #2
bartonc
6,596 Expert 4TB
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():
Expand|Select|Wrap|Line Numbers
  1. >>> aDict = dict(a=1, b=2, c=3)
  2. >>> bDict = {}
  3. >>> bDict.update(aDict)
  4. >>> bDict['d'] = 4
  5. >>> aDict
  6. {'a': 1, 'c': 3, 'b': 2}
  7. >>> bDict
  8. {'a': 1, 'c': 3, 'b': 2, 'd': 4}
  9. >>> 
Apr 19 '07 #3
psbasha
440 256MB
Which is faster ,whether assigning the variables directlyor using the methods for assigning the variables?

-PSB
Apr 19 '07 #4
bartonc
6,596 Expert 4TB
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.
Apr 19 '07 #5
psbasha
440 256MB
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.

Expand|Select|Wrap|Line Numbers
  1. Sample
  2.  
  3. >>> t1 = timeit.Timer("aDict = dict(a=1, b=2, c=3);bDict =aDict")
  4. >>> print t1.timeit()
  5. 1.89162365608
  6. >>> 
  7. t2 = timeit.Timer("aDict = dict(a=1, b=2, c=3);bDict = {};bDict.update(aDict)")
  8. >>> print t2.timeit()
  9. 3.35477972759
  10.  
Apr 20 '07 #6
bartonc
6,596 Expert 4TB
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.

Expand|Select|Wrap|Line Numbers
  1. Sample
  2.  
  3. >>> t1 = timeit.Timer("aDict = dict(a=1, b=2, c=3);bDict =aDict")
  4. >>> print t1.timeit()
  5. 1.89162365608
  6. >>> 
  7. t2 = timeit.Timer("aDict = dict(a=1, b=2, c=3);bDict = {};bDict.update(aDict)")
  8. >>> print t2.timeit()
  9. 3.35477972759
  10.  
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.
Apr 20 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

3
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...
5
by: carrajo | last post by:
Hey, I'm trying to duplicate the following: Select List 1 --- Apple Orange Banana
1
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...
4
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...
2
subashini Thiyagarajan
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...
6
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...
6
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...
8
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...
1
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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...
0
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
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
jinu1996
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.