How to make unique lists in python ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 8

How to make unique lists in python ?

Suppose a situation: >>> a=[1, 2, 3, 4] >>> b=a >>> a.append(5) >>> b [1, 2, 3, 4, 5] Oh no :/ How to prevent this and print early value of b a=[1, 2, 3, 4] b=[] b.append(a) a+=[5, 6, 7] b [[1, 2, 3, 4, 5, 6, 7]] This don't work either :/

28th Jun 2018, 5:37 PM
ᏳoƊoԲᎮc !
ᏳoƊoԲᎮc ! - avatar
5 Answers
+ 9
Thanks @Rahul_Verma Thanks Maninder Singh both work 👍
28th Jun 2018, 5:51 PM
ᏳoƊoԲᎮc !
ᏳoƊoԲᎮc ! - avatar
+ 8
The list function would do the work. b = list(a) Or alternatively, you can slice it too b = a[:]
28th Jun 2018, 5:48 PM
777
777 - avatar
+ 8
[] this creats lists in python and {} creats sets in python which is another thing to store data same like sets in maths In sets, Duplicates are not allowed so it can be used in comparison of two sets or lists by making them sets Alphabets are printed in ascending order a-z Note : {} is also used to make dictionary in {key:value} format In sets u can just add values {1, 2, 3, "four"}
8th Jul 2018, 6:18 PM
ᏳoƊoԲᎮc !
ᏳoƊoԲᎮc ! - avatar
28th Jun 2018, 5:48 PM
Maninder $ingh
Maninder $ingh - avatar
+ 2
{} different [] in the list ?? we knows that code: list=[1,3,12,50] with command : for i in list: print(i) then result = 1,3,12,50 but i change [] by {} then result is 1,50,3,12. I dont know why is that??Who explain help me???Thank so good! 🤔
8th Jul 2018, 5:43 PM
Thái Phạm
Thái Phạm - avatar