Avatar ·

How to change values in a list without creating a new list?

📁 Advanced Python Topics, work

The standard way using list comprehension:

my_list=[1,2,3,4,5,6]my_list_1=[i*5 for i in my_list]print(my_list_1)

Why can't I do the same with the original list:

my_list=[1,2,3,4,5,6]for i in my_list:    i*=5                    
Avatar ·

:

import randomitems = [random.randint(0, 100000) for i in range(100000)]print(len(items))  # 100000

If you only need unique numbers within the specified range, then you can simply generate all numbers from the range and shuffle them:

import randomitems = list(range(100000))print(items[:5])  # [0, 1, 2, 3, 4]

Log in to leave an answer

Blogs