+ 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 :/
5 ответов
+ 9
Thanks @Rahul_Verma
Thanks Maninder Singh
both work 👍
+ 8
The list function would do the work.
b = list(a)
Or alternatively, you can slice it too
b = a[:]
+ 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"}
+ 7
See this code.
https://code.sololearn.com/cqGaaSJ7g9bv/?ref=app
+ 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! 🤔