6

I know that the list() constructor creates a new list but what exactly are its characteristics?

  1. What happens when you call list((1,2,3,4,[5,6,7,8],9))?

  2. What happens when you call list([[[2,3,4]]])?

  3. What happens when you call list([[1,2,3],[4,5,6]])?

From what I can tell, calling the constructor list removes the most outer braces (tuple or list) and replaces them with []. Is this true? What other nuances does list() have?

4
  • 4
    What's stopping you from trying these things out in an interpreter? The results should be pretty self-evident. Commented Oct 27, 2014 at 5:05
  • 1
    list() Commented Oct 27, 2014 at 5:07
  • 1
    because i don't know if there are any other esoteric nuances for the constructor. Something that you should have been able to read from the question. @skrrgwasme Commented Oct 27, 2014 at 5:07
  • 2
    In general, one of the nicer things about python is that you don't have to worry about esoteric nuances for built-in types. You'll do better if you just spend time actually using the built-in types, and trying to utilize their relative arcana when you find they fall short of your use cases. Commented Oct 27, 2014 at 5:13

6 Answers 6

10

list() converts the iterable passed to it to a list. If the itertable is already a list then a shallow copy is returned, i.e only the outermost container is new rest of the objects are still the same.

>>> t = (1,2,3,4,[5,6,7,8],9)
>>> lst = list(t) 
>>> lst[4] is t[4]  #outermost container is now a list() but inner items are still same.
True

>>> lst1 = [[[2,3,4]]]
>>> id(lst1)
140270501696936
>>> lst2 = list(lst1)
>>> id(lst2)
140270478302096
>>> lst1[0] is lst2[0]
True
Sign up to request clarification or add additional context in comments.

6 Comments

so in english: list removes the outer braces and replaces them with [] and the inner items are still the same identity as before. correct?
Also, what does _ do in your 4th line?
@ylun.ca For tuples you can say so, for lists it is equivalent to slicing([:]). Note that it works with any iterable/iterator, so it's more than just replacing braces. For iterator it will consume the whole iterator and return a list of its items, for dict it'll return list of its keys etc.
@ylun.ca _ refers to the previous object in Python shell(I've removed it for now for the sake of clearity).
@ylun.ca Considering t is a list as well(because tuples are immutable themselves), then that won't affect lst[0] as you're modifying a completely different container. Assignment operators means the pointer at 0th index in this container now will point to 100, and that's not going to affect the other container. But if you try to update the list at index 4(say lst[4][0] = 100) then it will be reflected in both t and lst as they both contain references to the same list object. Related: Python list([]) and []
|
3

Python has a well-established documentation set for every release version, readable at https://docs.python.org/. The documentation for list() states that list() is merely a way of constructing a list object, of which these are the listed ways:

  • Using a pair of square brackets to denote the empty list: []
  • Using square brackets, separating items with commas: [a], [a, b, c]
  • Using a list comprehension: [x for x in iterable]
  • Using the type constructor: list() or list(iterable)

The list() function accepts any iterable as its argument, and the return value is a list object.

Further reading: https://docs.python.org/3.4/library/stdtypes.html#typesseq-list

1 Comment

I am looking for what the list constructor does, not how to create a list. What are its behaviours? what steps does the function take to create a list?
1

Yes it is true.

Its very simple. list() takes an iterable object as input and adds its elements to a newly created list. Elements can be anything. It can also be an another list or an iterable object, and it will be added to the new list as it is.

i.e no nested processing will happen.

Comments

1

You said: "From what I can tell, calling the constructor list removes the most outer braces (tuple or list) and replaces them with []. Is this true?"

IMHO, this is not a good way to think about what list() does. True, square brackets [] are used to write a list literal, and are used when you tell a list to represent itself as a string, but ultimately, that's just notation. It's better to think of a Python list as a particular kind of container object with certain properties, eg it's ordered, indexable, iterable, mutable, etc.

Thinking of the list() constructor in terms of it performing a transformation on the kind of brackets of a tuple that you pass it is a bit like saying adding 3 to 6 turns the 6 upside down to make 9. It's true that a '9' glyph looks like a '6' glyph turned upside down, but that's got nothing to do with what happens on the arithmetic level, and it's not even true of all fonts.

Comments

1
aTuple = (123, 'xyz', 'zara', 'abc');
aList = list(aTuple)

print "List elements : ", aList

When we run above program, it produces following result:

List elements :  [123, 'xyz', 'zara', 'abc']

It is another way to create a list in python. How convenient!

Comments

0

Your question is vague, but this is the output as follows, it doesn't "replace" the outer braces, it creates a data structure of a list, that can contain any value in a "listed" order (one after the other, after the other, and so on...) in a recursive way, you can add/remove elements to a specified index using append and pop. By the other hand, tuples are static and are not dynamically linked, they are more like an array of any type of element.

WHEN:

list((1,2,3,4,[5,6,7,8],9))

RETURNS:

[1, 2, 3, 4, [5, 6, 7, 8], 9]

WHEN:

list([[[2,3,4]]])

RETURNS:

[[[2, 3, 4]]]

WHEN:

list([[1,2,3],[4,5,6]])

RETURNS:

[[1, 2, 3], [4, 5, 6]]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.