How to delete list item automatically in python?
If i have a list
for example :
num=[1,2,3,4]
and i want to delete
list[0]
every 2 second, so after 2 second list in num is num = [2,3,4]
and 2 second after it will be num =[2,3]
how can i do that?
python
add a comment |
If i have a list
for example :
num=[1,2,3,4]
and i want to delete
list[0]
every 2 second, so after 2 second list in num is num = [2,3,4]
and 2 second after it will be num =[2,3]
how can i do that?
python
add a comment |
If i have a list
for example :
num=[1,2,3,4]
and i want to delete
list[0]
every 2 second, so after 2 second list in num is num = [2,3,4]
and 2 second after it will be num =[2,3]
how can i do that?
python
If i have a list
for example :
num=[1,2,3,4]
and i want to delete
list[0]
every 2 second, so after 2 second list in num is num = [2,3,4]
and 2 second after it will be num =[2,3]
how can i do that?
python
python
edited Dec 27 '18 at 15:40
yatu
6,8061826
6,8061826
asked Dec 27 '18 at 15:32
TiffanyTiffany
556
556
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
You could do it using time.sleep
, and del
to remove the first element:
for i in range(len(num)):
time.sleep(2)
del num[0]
Additionally, if you also want to do something with that first value, you could usenum.pop(0)
– KuboMD
Dec 27 '18 at 15:58
1
Also, although performance is probably not an issue here, for a long list you might want to use collections.deque instead of a list: docs.python.org/3.5/library/collections.html#collections.deque
– Gallaecio
Dec 30 '18 at 10:57
add a comment |
Another solution is using list.pop()
, as follows:
import time
num=[1,2,3,4]
while num:
num.pop(0)
time.sleep(2)
add a comment |
This does so, until eventually it runs out of elements to remove.
import time
num = [1, 2, 3, 4]
while num:
time.sleep(2)
del num[0]
print (num)
7
Might be tempted to make thatwhile num
instead ofwhile True
- that way the loop will exit gracefully (or not attempt to delete from an empty list to start with) when it's empty rather than raise an exception.
– Jon Clements♦
Dec 27 '18 at 15:41
Good idea. Done
– Joshua Fox
Dec 27 '18 at 19:19
add a comment |
Try this code !
Use sleep
function by importing time
file & del
command to remove the element from list.
Code :
import time
num = [1,2,3,4]
for i in num:
time.sleep(2)
print("Delete item ", i)
del i
Output :
Delete item 1
Delete item 2
Delete item 3
Delete item 4
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53947391%2fhow-to-delete-list-item-automatically-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could do it using time.sleep
, and del
to remove the first element:
for i in range(len(num)):
time.sleep(2)
del num[0]
Additionally, if you also want to do something with that first value, you could usenum.pop(0)
– KuboMD
Dec 27 '18 at 15:58
1
Also, although performance is probably not an issue here, for a long list you might want to use collections.deque instead of a list: docs.python.org/3.5/library/collections.html#collections.deque
– Gallaecio
Dec 30 '18 at 10:57
add a comment |
You could do it using time.sleep
, and del
to remove the first element:
for i in range(len(num)):
time.sleep(2)
del num[0]
Additionally, if you also want to do something with that first value, you could usenum.pop(0)
– KuboMD
Dec 27 '18 at 15:58
1
Also, although performance is probably not an issue here, for a long list you might want to use collections.deque instead of a list: docs.python.org/3.5/library/collections.html#collections.deque
– Gallaecio
Dec 30 '18 at 10:57
add a comment |
You could do it using time.sleep
, and del
to remove the first element:
for i in range(len(num)):
time.sleep(2)
del num[0]
You could do it using time.sleep
, and del
to remove the first element:
for i in range(len(num)):
time.sleep(2)
del num[0]
answered Dec 27 '18 at 15:35
yatuyatu
6,8061826
6,8061826
Additionally, if you also want to do something with that first value, you could usenum.pop(0)
– KuboMD
Dec 27 '18 at 15:58
1
Also, although performance is probably not an issue here, for a long list you might want to use collections.deque instead of a list: docs.python.org/3.5/library/collections.html#collections.deque
– Gallaecio
Dec 30 '18 at 10:57
add a comment |
Additionally, if you also want to do something with that first value, you could usenum.pop(0)
– KuboMD
Dec 27 '18 at 15:58
1
Also, although performance is probably not an issue here, for a long list you might want to use collections.deque instead of a list: docs.python.org/3.5/library/collections.html#collections.deque
– Gallaecio
Dec 30 '18 at 10:57
Additionally, if you also want to do something with that first value, you could use
num.pop(0)
– KuboMD
Dec 27 '18 at 15:58
Additionally, if you also want to do something with that first value, you could use
num.pop(0)
– KuboMD
Dec 27 '18 at 15:58
1
1
Also, although performance is probably not an issue here, for a long list you might want to use collections.deque instead of a list: docs.python.org/3.5/library/collections.html#collections.deque
– Gallaecio
Dec 30 '18 at 10:57
Also, although performance is probably not an issue here, for a long list you might want to use collections.deque instead of a list: docs.python.org/3.5/library/collections.html#collections.deque
– Gallaecio
Dec 30 '18 at 10:57
add a comment |
Another solution is using list.pop()
, as follows:
import time
num=[1,2,3,4]
while num:
num.pop(0)
time.sleep(2)
add a comment |
Another solution is using list.pop()
, as follows:
import time
num=[1,2,3,4]
while num:
num.pop(0)
time.sleep(2)
add a comment |
Another solution is using list.pop()
, as follows:
import time
num=[1,2,3,4]
while num:
num.pop(0)
time.sleep(2)
Another solution is using list.pop()
, as follows:
import time
num=[1,2,3,4]
while num:
num.pop(0)
time.sleep(2)
edited Dec 27 '18 at 16:17
answered Dec 27 '18 at 15:48
SseinSsein
1,0141921
1,0141921
add a comment |
add a comment |
This does so, until eventually it runs out of elements to remove.
import time
num = [1, 2, 3, 4]
while num:
time.sleep(2)
del num[0]
print (num)
7
Might be tempted to make thatwhile num
instead ofwhile True
- that way the loop will exit gracefully (or not attempt to delete from an empty list to start with) when it's empty rather than raise an exception.
– Jon Clements♦
Dec 27 '18 at 15:41
Good idea. Done
– Joshua Fox
Dec 27 '18 at 19:19
add a comment |
This does so, until eventually it runs out of elements to remove.
import time
num = [1, 2, 3, 4]
while num:
time.sleep(2)
del num[0]
print (num)
7
Might be tempted to make thatwhile num
instead ofwhile True
- that way the loop will exit gracefully (or not attempt to delete from an empty list to start with) when it's empty rather than raise an exception.
– Jon Clements♦
Dec 27 '18 at 15:41
Good idea. Done
– Joshua Fox
Dec 27 '18 at 19:19
add a comment |
This does so, until eventually it runs out of elements to remove.
import time
num = [1, 2, 3, 4]
while num:
time.sleep(2)
del num[0]
print (num)
This does so, until eventually it runs out of elements to remove.
import time
num = [1, 2, 3, 4]
while num:
time.sleep(2)
del num[0]
print (num)
edited Dec 27 '18 at 19:19
answered Dec 27 '18 at 15:38
Joshua FoxJoshua Fox
7,92495178
7,92495178
7
Might be tempted to make thatwhile num
instead ofwhile True
- that way the loop will exit gracefully (or not attempt to delete from an empty list to start with) when it's empty rather than raise an exception.
– Jon Clements♦
Dec 27 '18 at 15:41
Good idea. Done
– Joshua Fox
Dec 27 '18 at 19:19
add a comment |
7
Might be tempted to make thatwhile num
instead ofwhile True
- that way the loop will exit gracefully (or not attempt to delete from an empty list to start with) when it's empty rather than raise an exception.
– Jon Clements♦
Dec 27 '18 at 15:41
Good idea. Done
– Joshua Fox
Dec 27 '18 at 19:19
7
7
Might be tempted to make that
while num
instead of while True
- that way the loop will exit gracefully (or not attempt to delete from an empty list to start with) when it's empty rather than raise an exception.– Jon Clements♦
Dec 27 '18 at 15:41
Might be tempted to make that
while num
instead of while True
- that way the loop will exit gracefully (or not attempt to delete from an empty list to start with) when it's empty rather than raise an exception.– Jon Clements♦
Dec 27 '18 at 15:41
Good idea. Done
– Joshua Fox
Dec 27 '18 at 19:19
Good idea. Done
– Joshua Fox
Dec 27 '18 at 19:19
add a comment |
Try this code !
Use sleep
function by importing time
file & del
command to remove the element from list.
Code :
import time
num = [1,2,3,4]
for i in num:
time.sleep(2)
print("Delete item ", i)
del i
Output :
Delete item 1
Delete item 2
Delete item 3
Delete item 4
add a comment |
Try this code !
Use sleep
function by importing time
file & del
command to remove the element from list.
Code :
import time
num = [1,2,3,4]
for i in num:
time.sleep(2)
print("Delete item ", i)
del i
Output :
Delete item 1
Delete item 2
Delete item 3
Delete item 4
add a comment |
Try this code !
Use sleep
function by importing time
file & del
command to remove the element from list.
Code :
import time
num = [1,2,3,4]
for i in num:
time.sleep(2)
print("Delete item ", i)
del i
Output :
Delete item 1
Delete item 2
Delete item 3
Delete item 4
Try this code !
Use sleep
function by importing time
file & del
command to remove the element from list.
Code :
import time
num = [1,2,3,4]
for i in num:
time.sleep(2)
print("Delete item ", i)
del i
Output :
Delete item 1
Delete item 2
Delete item 3
Delete item 4
answered Dec 27 '18 at 16:02
UsmanUsman
754614
754614
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53947391%2fhow-to-delete-list-item-automatically-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown