469,632 Members | 1,632 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,632 developers. It's quick & easy.

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 2780
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

Post your reply

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

Similar topics

3 posts views Thread by Ricky W. Hunt | last post: by
5 posts views Thread by carrajo | last post: by
1 post views Thread by matthew.macdonald-wallace | last post: by
6 posts views Thread by Tem | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.