python lists, lists in python, python
Python hosting: Host, run, and code Python in the cloud!
Lists is a sequence and a basic data structure. A list may contain strings (text) and numbers. A list is similar to an array in other programming languages, but has additional functionality.
Python Programming Bootcamp: Go from zero to hero
We define lists with brackets . To access the data, these same brackets are used.
Example list usage:
We can use the functions append() and remove() to manipulate the list.
We can sort the list using the sort() function.
If you want to have the list in descending order, simply use the reverse() function.
If you are new to Python programming, I highly recommend this book.
Posted in beginner
Leave a Reply:
wow this is interest
Can ya make a 2D and 3D array too?
Yes, there is no limit to the number of dimensions in an array.
a 2d array:
a 3d array:
when i am trying for this 3D array i am getting below error:
i have python35-32 installed on my PC and when i am trying to run this i am getting above error.
Try changing to:
what is difference between list = [[0, 1], [1, 2], [2, 3]] and list = ([0, 1], [1, 2], [2, 3]) both appears to be array?
Good question! The second is a tuple, not a list. A tuple is a collection of variables that cannot be changed.
Once you try to set one of the tuple variables, you will get an exception. Try with this code:
When I type
it gives an error:
SyntaxError: Missing parentheses in call to 'print'
Hi, you are using Python 3.x. Use: print(l)
Did anyone can tell me why
Hi Ben, this will work:
This is because reverse() returns None (see the function call paragraph). The function call changes its internal list but does not return the new list.
Some more examples:
Thank you so much . this function different from PHP/PERL
a.reverse function will perform the reverse operation, it will store in the variable,
so call the variable then you got the answer ok
I have a similar problem.
I defined the variable first as n.
When I command "print (n.sort())" or "print (n.reverse())" it responds "None" each time.
Call n.sort() or n.reverse() then print(n)
But it showed : File "C:\Python27\learn\test.py", line 48, in
TypeError: 'builtin_function_or_method' object has no attribute '__getitem__'?
why I can't via loops to append value to a list?
Hi Ben, use brackets for append. Like this:
hey having issues with this part
im on python 3 i know nothing about this im just here trying to learn any help is apreciated
In Python 3.x the brackets are required:
if when i try print l and it doesnt work is it because im using python 3>?
thank you sir , if im a beginner and am going thru tutorials would you recommend i download the other version ?
If you use brackets you can use Python 3.x.
All of them should work
Yes. Use print(l) for Python 3.x
i dont think its because of the python version..im using python 3 and its working very well even vat that part
when I run this code on "http://pythonspot.com/run.php" both of them is the same??
but when I run local, I get an error:
TypeError: 'tuple' object does not support item assignment
Why don't use:
The first one defines a list, the second one a tuple. Tuples cannot be given new values. You can read more on tuples here.
That should work too, only difference is first one is dynamic second static. It depends on your application
pyhton 2.7.10 is ok
Is there any way to print like this without using any loop? I'm using Python 3.x
Loops are preferred but yes, you can print them one by one:
There is another method, but loops would create the cleanest code.
How can i print the items in the list randomly
You can use the random.shuffle() method: