Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: What does the use of [] mean here?

What is the difference in these two statements in python?

var = foo.bar

and

var = [foo.bar]

I think it is making var into a list containing foo.bar but I am unsure. Also if this is the behavior and foo.bar is already a list what do you get in each case?

For example: if foo.bar = [1, 2] would I get this?

var = foo.bar #[1, 2]

and

var = [foo.bar] #[[1,2]] where [1,2] is the first element in a multidimensional list
like image 732
Andrew Hubbs Avatar asked Sep 21 '10 22:09

Andrew Hubbs


People also ask

What does // mean in Python?

What Does // Mean in Python? Operators in Python In Python, you use the double slash // operator to perform floor division. This // operator divides the first number by the second number and rounds the result down to the nearest integer (or whole number).

What is the meaning of not in Python?

(a or b) is true. Used to reverse the logical state of its operand. Not (a and b) is false. Python’s membership operators test for membership in a sequence, such as strings, lists, or tuples. There are two membership operators as explained below −

What is Python?

Python is a high-level, general-purpose programming language, that aims to produce a clearer and more logical code for both small and large-scale projects. Python is surprisingly easy to read and, as an interpreted language, it does not transform code to become computer-readable.

What are the operators used in Python?

Operators are used to perform operations on variables and values. Python divides the operators in the following groups: Arithmetic operators. Assignment operators. Comparison operators.


3 Answers

[] is an empty list.

[foo.bar] is creating a new list ([]) with foo.bar as the first item in the list, which can then be referenced by its index:

var = [foo.bar]
var[0] == foo.bar # returns True 

So your guess that your assignment of foo.bar = [1,2] is exactly right.

If you haven't already, I recommend playing around with this kind of thing in the Python interactive interpreter. It makes it pretty easy:

>>> []
[]
>>> foobar = [1,2]
>>> foobar
[1, 2]
>>> [foobar]
[[1, 2]]
like image 78
jathanism Avatar answered Oct 14 '22 00:10

jathanism


Yes, it's making a list containing one element, foo.bar.

If foo.bar is [1,2], you indeed get [[1,2]].

For instance,

>> a=[]
>> a.append([1,2])
>> a[0] 
[1,2]
>> b=[[1,2]]
>> b[0]
[1,2]

To elaborate a bit more on that exact example,

>> class Foos:
>>   bar=[1,2]
>> foo=Foos()
>> foo.bar
[1,2]
>> a=[foo.bar]
>> a
[[1,2]]
>> a[0]
[1,2]
like image 45
JAL Avatar answered Oct 14 '22 00:10

JAL


I think it is making var into a list containing foo.bar but I am unsure. Also if this is the behavior and foo.bar is already a list what do you get in each case?

  • Yes, it creates a new list.

  • If foo.bar is already a list, it will simply become a list, containing one list.

    h[1] >>> l = [1, 2]
    h[1] >>> [l]
    [[1, 2]]
    h[3] >>> l[l][0]
    [1, 2]
    
like image 1
miku Avatar answered Oct 14 '22 02:10

miku