Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of list[:] in this code? [duplicate]

This code is from Python's Documentation. I'm a little confused.

words = ['cat', 'window', 'defenestrate'] for w in words[:]:     if len(w) > 6:         words.insert(0, w) print(words) 

And the following is what I thought at first:

words = ['cat', 'window', 'defenestrate'] for w in words:     if len(w) > 6:         words.insert(0, w) print(words) 

Why does this code create a infinite loop and the first one doesn't?

like image 855
S. Li Avatar asked Jun 19 '17 14:06

S. Li


People also ask

What is the meaning of list [: in Python?

List. Lists are used to store multiple items in a single variable. Lists are one of 4 built-in data types in Python used to store collections of data, the other 3 are Tuple, Set, and Dictionary, all with different qualities and usage.

What does list [:] do in Python?

Definition and Usage. The list() function creates a list object. A list object is a collection which is ordered and changeable. Read more about list in the chapter: Python Lists.

What does [- 1 :] mean in Python?

Python also allows you to index from the end of the list using a negative number, where [-1] returns the last element. This is super-useful since it means you don't have to programmatically find out the length of the iterable in order to work with elements at the end of it.

What does [:- 1 in Python do?

In Python, [::-1] means reversing a string, list, or any iterable with an ordering. This is the quick answer.


1 Answers

This is one of the gotchas! of python, that can escape beginners.

The words[:] is the magic sauce here.

Observe:

>>> words =  ['cat', 'window', 'defenestrate'] >>> words2 = words[:] >>> words2.insert(0, 'hello') >>> words2 ['hello', 'cat', 'window', 'defenestrate'] >>> words ['cat', 'window', 'defenestrate'] 

And now without the [:]:

>>> words =  ['cat', 'window', 'defenestrate'] >>> words2 = words >>> words2.insert(0, 'hello') >>> words2 ['hello', 'cat', 'window', 'defenestrate'] >>> words ['hello', 'cat', 'window', 'defenestrate'] 

The main thing to note here is that words[:] returns a copy of the existing list, so you are iterating over a copy, which is not modified.

You can check whether you are referring to the same lists using id():

In the first case:

>>> words2 = words[:] >>> id(words2) 4360026736 >>> id(words) 4360188992 >>> words2 is words False 

In the second case:

>>> id(words2) 4360188992 >>> id(words) 4360188992 >>> words2 is words True 

It is worth noting that [i:j] is called the slicing operator, and what it does is it returns a fresh copy of the list starting from index i, upto (but not including) index j.

So, words[0:2] gives you

>>> words[0:2] ['hello', 'cat'] 

Omitting the starting index means it defaults to 0, while omitting the last index means it defaults to len(words), and the end result is that you receive a copy of the entire list.


If you want to make your code a little more readable, I recommend the copy module.

from copy import copy   words = ['cat', 'window', 'defenestrate'] for w in copy(words):     if len(w) > 6:         words.insert(0, w) print(words) 

This basically does the same thing as your first code snippet, and is much more readable.

Alternatively (as mentioned by DSM in the comments) and on python >=3, you may also use words.copy() which does the same thing.

like image 114
cs95 Avatar answered Sep 23 '22 07:09

cs95