449,079 Members | 898 Online 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
6 Replies

 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

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

 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 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   Apr 20 '07 #6

 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 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. Apr 20 '07 #7 