By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,079 Members | 898 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,079 IT Pros & Developers. It's quick & easy.

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

100+
P: 440
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
Share this Question
Share on Google+
6 Replies


bartonc
Expert 5K+
P: 6,596
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
Expert 5K+
P: 6,596
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

100+
P: 440
Which is faster ,whether assigning the variables directlyor using the methods for assigning the variables?

-PSB
Apr 19 '07 #4

bartonc
Expert 5K+
P: 6,596
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

100+
P: 440
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
Expert 5K+
P: 6,596
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

Post your reply

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