How to access a dict's values and delete them?Is it Pythonic to use list comprehensions for just side effects?How to merge two dictionaries in a single expression?How do I efficiently iterate over each entry in a Java Map?How do I sort a list of dictionaries by a value of the dictionary?How do I check whether a file exists without exceptions?How do I return multiple values from a function?Accessing the index in 'for' loops?How do I sort a dictionary by value?How do I list all files of a directory?How to access environment variable values?Fastest way to check if a value exist in a list
Can the inductive kick be discharged without a freewheeling diode, in this example?
Best practice with Return in Module or Block
What is the practical impact of using System.Random which is not cryptographically random?
What's the origin of the concept of alternate dimensions/realities?
Is this statement about a motion being simple harmonic in nature strong?
Is there anything in the universe that cannot be compressed?
How smart contract transactions work?
Using font to highlight a god's speech in dialogue
Squares inside a square
Why do motor drives have multiple bus capacitors of small value capacitance instead of a single bus capacitor of large value?
Why do presidential pardons exist in a country having a clear separation of powers?
Divide Numbers by 0
Deck of Many Things. What happens if you don't declare any number of cards and just start drawing?
How to differentiate between two people with the same name in a story?
How to get frequency counts using column breaks by row?
Am I required to correct my opponent's assumptions about my morph creatures?
Given a specific computer system, is it possible to estimate the actual precise run time of a piece of Assembly code
I was given someone else's visa, stamped in my passport
Understanding data transmission rates over copper wire
Does Q ever actually lie?
How would a disabled person earn their living in a medieval-type town?
A word for the urge to do the opposite
Properly unlinking hard links
Moving DNS hosting for Active site to Route 53 - with G Suite MX TTL of 1 week
How to access a dict's values and delete them?
Is it Pythonic to use list comprehensions for just side effects?How to merge two dictionaries in a single expression?How do I efficiently iterate over each entry in a Java Map?How do I sort a list of dictionaries by a value of the dictionary?How do I check whether a file exists without exceptions?How do I return multiple values from a function?Accessing the index in 'for' loops?How do I sort a dictionary by value?How do I list all files of a directory?How to access environment variable values?Fastest way to check if a value exist in a list
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have a Python dict stuffs
with keys and values(list);
'car':['bmw','porsche','benz'] 'fruits':['banana','apple']
And I would like delete first value from cars: bmw
and first value from fruits: banana
How can I access and delete them please? I have tried .pop(index)
, but it doesn't work...
python python-3.x dictionary
add a comment |
I have a Python dict stuffs
with keys and values(list);
'car':['bmw','porsche','benz'] 'fruits':['banana','apple']
And I would like delete first value from cars: bmw
and first value from fruits: banana
How can I access and delete them please? I have tried .pop(index)
, but it doesn't work...
python python-3.x dictionary
What did you callpop
on?
– wbadart
Mar 27 at 23:53
What have you tried so far?
– dcg
Mar 27 at 23:53
@wbadart stuffs.pop(1), but it wont access that list
– Marcel Kopera
Mar 27 at 23:55
del stuffs['car'][0]
– juanpa.arrivillaga
Mar 28 at 0:05
add a comment |
I have a Python dict stuffs
with keys and values(list);
'car':['bmw','porsche','benz'] 'fruits':['banana','apple']
And I would like delete first value from cars: bmw
and first value from fruits: banana
How can I access and delete them please? I have tried .pop(index)
, but it doesn't work...
python python-3.x dictionary
I have a Python dict stuffs
with keys and values(list);
'car':['bmw','porsche','benz'] 'fruits':['banana','apple']
And I would like delete first value from cars: bmw
and first value from fruits: banana
How can I access and delete them please? I have tried .pop(index)
, but it doesn't work...
python python-3.x dictionary
python python-3.x dictionary
edited Mar 28 at 0:42
martineau
75.2k11 gold badges103 silver badges198 bronze badges
75.2k11 gold badges103 silver badges198 bronze badges
asked Mar 27 at 23:51
Marcel KoperaMarcel Kopera
15312 bronze badges
15312 bronze badges
What did you callpop
on?
– wbadart
Mar 27 at 23:53
What have you tried so far?
– dcg
Mar 27 at 23:53
@wbadart stuffs.pop(1), but it wont access that list
– Marcel Kopera
Mar 27 at 23:55
del stuffs['car'][0]
– juanpa.arrivillaga
Mar 28 at 0:05
add a comment |
What did you callpop
on?
– wbadart
Mar 27 at 23:53
What have you tried so far?
– dcg
Mar 27 at 23:53
@wbadart stuffs.pop(1), but it wont access that list
– Marcel Kopera
Mar 27 at 23:55
del stuffs['car'][0]
– juanpa.arrivillaga
Mar 28 at 0:05
What did you call
pop
on?– wbadart
Mar 27 at 23:53
What did you call
pop
on?– wbadart
Mar 27 at 23:53
What have you tried so far?
– dcg
Mar 27 at 23:53
What have you tried so far?
– dcg
Mar 27 at 23:53
@wbadart stuffs.pop(1), but it wont access that list
– Marcel Kopera
Mar 27 at 23:55
@wbadart stuffs.pop(1), but it wont access that list
– Marcel Kopera
Mar 27 at 23:55
del stuffs['car'][0]
– juanpa.arrivillaga
Mar 28 at 0:05
del stuffs['car'][0]
– juanpa.arrivillaga
Mar 28 at 0:05
add a comment |
3 Answers
3
active
oldest
votes
An easy way of doing this is to use a for loop and iterate over each item in you're dictionary, and pop the first element:
dictionary = 'car':['bmw','porsche','benz'], 'fruits':['banana','apple']
for key in dictionary:
dictionary[key].pop(0)
Or, as a list comprehension
dictionary = 'car':['bmw','porsche','benz'], 'fruits':['banana','apple']
[dictionary[i].pop(0) for i in dictionary]
These pieces of code reference the dictionary at each of it's keys ('car'
and 'fruits'
) and then proceeds to use pop
on the values indexed by these keys.
Edit:
Don't use a list comprehension if you don't intend to store the list. In the case where you are iterating over large values, you could run into memory errors due to storing a whole load of useless values. Such as in this case:
[print(i) for i in range(9823498)]
This will store 9823498 None
values*, where as a for loop would not. but still achieve the same thing.
Do not use list-comprehensions for side-effects.
– juanpa.arrivillaga
Mar 28 at 0:04
@juanpa.arrivillaga what do you mean side-effects?
– Recessive
Mar 28 at 0:07
You are building a useless list with that list comprehension. This is not good practice at all; list comprehensions are not substitutes forfor
loops.
– iz_
Mar 28 at 0:10
@Tomothy32 Just to be clear so anyone who sees this understands why it is bad practice, can you elaborate?
– Recessive
Mar 28 at 0:19
2
Take a look at this: stackoverflow.com/q/5753597
– iz_
Mar 28 at 0:22
|
show 2 more comments
You can create a new dictionary where you skip the first element using [1:]
stuffs = 'car':['bmw','porsche','benz'], 'fruits':['banana','apple']
stuffs_new = k:v[1:] for k,v in stuffs.items()
# 'car': ['porsche', 'benz'], 'fruits': ['apple']
thanks, it worked!
– Marcel Kopera
Mar 28 at 0:04
add a comment |
You were almost there.
Use either:
del dict[key]
Or
dict.pop(key, value)
The second will remove but also leave the item available as a return
2
This will delete an entire key, he only wants the first index of the value indexed by the key to be removed
– Recessive
Mar 27 at 23:59
1
@Recessive You are totally right, misinterpreted it. Even still, just the syntax of pop would be helpful for him I guess, considering it takes different arguments from what he had tried.
– EngineersBox
Mar 28 at 0:04
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%2f55388183%2fhow-to-access-a-dicts-values-and-delete-them%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
An easy way of doing this is to use a for loop and iterate over each item in you're dictionary, and pop the first element:
dictionary = 'car':['bmw','porsche','benz'], 'fruits':['banana','apple']
for key in dictionary:
dictionary[key].pop(0)
Or, as a list comprehension
dictionary = 'car':['bmw','porsche','benz'], 'fruits':['banana','apple']
[dictionary[i].pop(0) for i in dictionary]
These pieces of code reference the dictionary at each of it's keys ('car'
and 'fruits'
) and then proceeds to use pop
on the values indexed by these keys.
Edit:
Don't use a list comprehension if you don't intend to store the list. In the case where you are iterating over large values, you could run into memory errors due to storing a whole load of useless values. Such as in this case:
[print(i) for i in range(9823498)]
This will store 9823498 None
values*, where as a for loop would not. but still achieve the same thing.
Do not use list-comprehensions for side-effects.
– juanpa.arrivillaga
Mar 28 at 0:04
@juanpa.arrivillaga what do you mean side-effects?
– Recessive
Mar 28 at 0:07
You are building a useless list with that list comprehension. This is not good practice at all; list comprehensions are not substitutes forfor
loops.
– iz_
Mar 28 at 0:10
@Tomothy32 Just to be clear so anyone who sees this understands why it is bad practice, can you elaborate?
– Recessive
Mar 28 at 0:19
2
Take a look at this: stackoverflow.com/q/5753597
– iz_
Mar 28 at 0:22
|
show 2 more comments
An easy way of doing this is to use a for loop and iterate over each item in you're dictionary, and pop the first element:
dictionary = 'car':['bmw','porsche','benz'], 'fruits':['banana','apple']
for key in dictionary:
dictionary[key].pop(0)
Or, as a list comprehension
dictionary = 'car':['bmw','porsche','benz'], 'fruits':['banana','apple']
[dictionary[i].pop(0) for i in dictionary]
These pieces of code reference the dictionary at each of it's keys ('car'
and 'fruits'
) and then proceeds to use pop
on the values indexed by these keys.
Edit:
Don't use a list comprehension if you don't intend to store the list. In the case where you are iterating over large values, you could run into memory errors due to storing a whole load of useless values. Such as in this case:
[print(i) for i in range(9823498)]
This will store 9823498 None
values*, where as a for loop would not. but still achieve the same thing.
Do not use list-comprehensions for side-effects.
– juanpa.arrivillaga
Mar 28 at 0:04
@juanpa.arrivillaga what do you mean side-effects?
– Recessive
Mar 28 at 0:07
You are building a useless list with that list comprehension. This is not good practice at all; list comprehensions are not substitutes forfor
loops.
– iz_
Mar 28 at 0:10
@Tomothy32 Just to be clear so anyone who sees this understands why it is bad practice, can you elaborate?
– Recessive
Mar 28 at 0:19
2
Take a look at this: stackoverflow.com/q/5753597
– iz_
Mar 28 at 0:22
|
show 2 more comments
An easy way of doing this is to use a for loop and iterate over each item in you're dictionary, and pop the first element:
dictionary = 'car':['bmw','porsche','benz'], 'fruits':['banana','apple']
for key in dictionary:
dictionary[key].pop(0)
Or, as a list comprehension
dictionary = 'car':['bmw','porsche','benz'], 'fruits':['banana','apple']
[dictionary[i].pop(0) for i in dictionary]
These pieces of code reference the dictionary at each of it's keys ('car'
and 'fruits'
) and then proceeds to use pop
on the values indexed by these keys.
Edit:
Don't use a list comprehension if you don't intend to store the list. In the case where you are iterating over large values, you could run into memory errors due to storing a whole load of useless values. Such as in this case:
[print(i) for i in range(9823498)]
This will store 9823498 None
values*, where as a for loop would not. but still achieve the same thing.
An easy way of doing this is to use a for loop and iterate over each item in you're dictionary, and pop the first element:
dictionary = 'car':['bmw','porsche','benz'], 'fruits':['banana','apple']
for key in dictionary:
dictionary[key].pop(0)
Or, as a list comprehension
dictionary = 'car':['bmw','porsche','benz'], 'fruits':['banana','apple']
[dictionary[i].pop(0) for i in dictionary]
These pieces of code reference the dictionary at each of it's keys ('car'
and 'fruits'
) and then proceeds to use pop
on the values indexed by these keys.
Edit:
Don't use a list comprehension if you don't intend to store the list. In the case where you are iterating over large values, you could run into memory errors due to storing a whole load of useless values. Such as in this case:
[print(i) for i in range(9823498)]
This will store 9823498 None
values*, where as a for loop would not. but still achieve the same thing.
edited Mar 28 at 0:39
answered Mar 27 at 23:57
RecessiveRecessive
5454 silver badges18 bronze badges
5454 silver badges18 bronze badges
Do not use list-comprehensions for side-effects.
– juanpa.arrivillaga
Mar 28 at 0:04
@juanpa.arrivillaga what do you mean side-effects?
– Recessive
Mar 28 at 0:07
You are building a useless list with that list comprehension. This is not good practice at all; list comprehensions are not substitutes forfor
loops.
– iz_
Mar 28 at 0:10
@Tomothy32 Just to be clear so anyone who sees this understands why it is bad practice, can you elaborate?
– Recessive
Mar 28 at 0:19
2
Take a look at this: stackoverflow.com/q/5753597
– iz_
Mar 28 at 0:22
|
show 2 more comments
Do not use list-comprehensions for side-effects.
– juanpa.arrivillaga
Mar 28 at 0:04
@juanpa.arrivillaga what do you mean side-effects?
– Recessive
Mar 28 at 0:07
You are building a useless list with that list comprehension. This is not good practice at all; list comprehensions are not substitutes forfor
loops.
– iz_
Mar 28 at 0:10
@Tomothy32 Just to be clear so anyone who sees this understands why it is bad practice, can you elaborate?
– Recessive
Mar 28 at 0:19
2
Take a look at this: stackoverflow.com/q/5753597
– iz_
Mar 28 at 0:22
Do not use list-comprehensions for side-effects.
– juanpa.arrivillaga
Mar 28 at 0:04
Do not use list-comprehensions for side-effects.
– juanpa.arrivillaga
Mar 28 at 0:04
@juanpa.arrivillaga what do you mean side-effects?
– Recessive
Mar 28 at 0:07
@juanpa.arrivillaga what do you mean side-effects?
– Recessive
Mar 28 at 0:07
You are building a useless list with that list comprehension. This is not good practice at all; list comprehensions are not substitutes for
for
loops.– iz_
Mar 28 at 0:10
You are building a useless list with that list comprehension. This is not good practice at all; list comprehensions are not substitutes for
for
loops.– iz_
Mar 28 at 0:10
@Tomothy32 Just to be clear so anyone who sees this understands why it is bad practice, can you elaborate?
– Recessive
Mar 28 at 0:19
@Tomothy32 Just to be clear so anyone who sees this understands why it is bad practice, can you elaborate?
– Recessive
Mar 28 at 0:19
2
2
Take a look at this: stackoverflow.com/q/5753597
– iz_
Mar 28 at 0:22
Take a look at this: stackoverflow.com/q/5753597
– iz_
Mar 28 at 0:22
|
show 2 more comments
You can create a new dictionary where you skip the first element using [1:]
stuffs = 'car':['bmw','porsche','benz'], 'fruits':['banana','apple']
stuffs_new = k:v[1:] for k,v in stuffs.items()
# 'car': ['porsche', 'benz'], 'fruits': ['apple']
thanks, it worked!
– Marcel Kopera
Mar 28 at 0:04
add a comment |
You can create a new dictionary where you skip the first element using [1:]
stuffs = 'car':['bmw','porsche','benz'], 'fruits':['banana','apple']
stuffs_new = k:v[1:] for k,v in stuffs.items()
# 'car': ['porsche', 'benz'], 'fruits': ['apple']
thanks, it worked!
– Marcel Kopera
Mar 28 at 0:04
add a comment |
You can create a new dictionary where you skip the first element using [1:]
stuffs = 'car':['bmw','porsche','benz'], 'fruits':['banana','apple']
stuffs_new = k:v[1:] for k,v in stuffs.items()
# 'car': ['porsche', 'benz'], 'fruits': ['apple']
You can create a new dictionary where you skip the first element using [1:]
stuffs = 'car':['bmw','porsche','benz'], 'fruits':['banana','apple']
stuffs_new = k:v[1:] for k,v in stuffs.items()
# 'car': ['porsche', 'benz'], 'fruits': ['apple']
answered Mar 27 at 23:55
SheldoreSheldore
22k5 gold badges15 silver badges37 bronze badges
22k5 gold badges15 silver badges37 bronze badges
thanks, it worked!
– Marcel Kopera
Mar 28 at 0:04
add a comment |
thanks, it worked!
– Marcel Kopera
Mar 28 at 0:04
thanks, it worked!
– Marcel Kopera
Mar 28 at 0:04
thanks, it worked!
– Marcel Kopera
Mar 28 at 0:04
add a comment |
You were almost there.
Use either:
del dict[key]
Or
dict.pop(key, value)
The second will remove but also leave the item available as a return
2
This will delete an entire key, he only wants the first index of the value indexed by the key to be removed
– Recessive
Mar 27 at 23:59
1
@Recessive You are totally right, misinterpreted it. Even still, just the syntax of pop would be helpful for him I guess, considering it takes different arguments from what he had tried.
– EngineersBox
Mar 28 at 0:04
add a comment |
You were almost there.
Use either:
del dict[key]
Or
dict.pop(key, value)
The second will remove but also leave the item available as a return
2
This will delete an entire key, he only wants the first index of the value indexed by the key to be removed
– Recessive
Mar 27 at 23:59
1
@Recessive You are totally right, misinterpreted it. Even still, just the syntax of pop would be helpful for him I guess, considering it takes different arguments from what he had tried.
– EngineersBox
Mar 28 at 0:04
add a comment |
You were almost there.
Use either:
del dict[key]
Or
dict.pop(key, value)
The second will remove but also leave the item available as a return
You were almost there.
Use either:
del dict[key]
Or
dict.pop(key, value)
The second will remove but also leave the item available as a return
answered Mar 27 at 23:55
EngineersBoxEngineersBox
581 silver badge5 bronze badges
581 silver badge5 bronze badges
2
This will delete an entire key, he only wants the first index of the value indexed by the key to be removed
– Recessive
Mar 27 at 23:59
1
@Recessive You are totally right, misinterpreted it. Even still, just the syntax of pop would be helpful for him I guess, considering it takes different arguments from what he had tried.
– EngineersBox
Mar 28 at 0:04
add a comment |
2
This will delete an entire key, he only wants the first index of the value indexed by the key to be removed
– Recessive
Mar 27 at 23:59
1
@Recessive You are totally right, misinterpreted it. Even still, just the syntax of pop would be helpful for him I guess, considering it takes different arguments from what he had tried.
– EngineersBox
Mar 28 at 0:04
2
2
This will delete an entire key, he only wants the first index of the value indexed by the key to be removed
– Recessive
Mar 27 at 23:59
This will delete an entire key, he only wants the first index of the value indexed by the key to be removed
– Recessive
Mar 27 at 23:59
1
1
@Recessive You are totally right, misinterpreted it. Even still, just the syntax of pop would be helpful for him I guess, considering it takes different arguments from what he had tried.
– EngineersBox
Mar 28 at 0:04
@Recessive You are totally right, misinterpreted it. Even still, just the syntax of pop would be helpful for him I guess, considering it takes different arguments from what he had tried.
– EngineersBox
Mar 28 at 0:04
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%2f55388183%2fhow-to-access-a-dicts-values-and-delete-them%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
What did you call
pop
on?– wbadart
Mar 27 at 23:53
What have you tried so far?
– dcg
Mar 27 at 23:53
@wbadart stuffs.pop(1), but it wont access that list
– Marcel Kopera
Mar 27 at 23:55
del stuffs['car'][0]
– juanpa.arrivillaga
Mar 28 at 0:05