Member-only story
7 Python Tips That Every Python Programmer Should Know
Python tips to code like a pro programmer
Python is a versatile and beginner-friendly programming language used in different fields from Machine Learning to Web development. It is famous among developers due to its easy syntax.
In this article, I’ll share and discuss some python tips and code snippets that can be used to solve day-to-day programming problems. Let’s get started!
1. Remove elements while looping
Let’s take an example of removing even numbers from a list. We have to perform this task without using another list. We can do it by iterating over the copy of the list.
arr = [1,2,3,4,4,7]for element in arr[:]:
if element%2==0:
arr.remove(element)print(arr)
Output:
[1,3,7]
In the above example, we have created a copy of the list using list slicing arr[:]
and using it we have modified the original list.
Another way:
arr = [1,2,3,4,4,7]arr[:] = [item for item in arr if item%2!=0]
print(arr)
Output:
[1,3,7]