python lists
Python hosting: Host, run, and code Python in the cloud!
Python lists are one of the most versatile and commonly used data structures. Let’s dive deep into understanding lists, their characteristics, and how to manipulate them effectively.
Introduction to Python Lists
Lists in Python are sequences and fall under the basic data structure category. They can hold a mixture of strings (text), numbers, and other data types. For those familiar with other programming languages, Python lists are analogous to arrays but offer enhanced features.
Recommended Course:
Python Programming Bootcamp: Go from zero to hero
Creating and Accessing Lists
Lists in Python are defined using square brackets []
. The same brackets are utilized to retrieve the data stored inside the lists.
Here’s a simple example demonstrating list creation and accessing its elements:
#!/usr/bin/python |
Manipulating Lists: Adding and Removing Elements
Python provides built-in functions like append()
and remove()
to add or eliminate items from a list, respectively.
Here’s an illustration of how to utilize these functions:
#!/usr/bin/python |
Sorting Lists: Ascending and Descending Order
To organize the items in a list, Python offers the sort()
function. This function arranges items in ascending order by default.
Let’s take a look at how this can be achieved:
#!/usr/bin/python |
If you wish to view the list in descending order, you can use the sort()
followed by the reverse()
function. Here’s how:#!/usr/bin/python
l = [ "Drake", "Derp", "Derek", "Dominique" ]
print(l) # prints all elements
l.sort() # sorts the list in alphabetical order
l.reverse() # reverse order.
print(l) # prints all elements
Additional Resources
For those keen to practice and enhance their Python skills, consider:
Download Python Exercises
Ready to move forward? Dive deep into the next topic or revisit previous lessons using the navigation links below.
Previous Lesson: Python Variables | Next Lesson: If Statements in Python
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:
Also try:
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:
or
Thank you so much . this function different from PHP/PERL
Hello ben,
a.reverse function will perform the reverse operation, it will store in the variable,
so call the variable then you got the answer ok
Regards,
Nagendra N
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)
Hi,
But it showed : File "C:\Python27\learn\test.py", line 48, in
a.append[i]
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
Hi Mike,
In Python 3.x the brackets are required:
Output:
if when i try print l[0] 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 ?
Hi Mike,
If you use brackets you can use Python 3.x.
All of them should work
Yes. Use print(l[0]) 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??
output is:
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: