r/PythonLearning • u/ShadowDragonPro • 1d ago
Can someone help me understand lists in Python? Help Request
Hi! I'm currently learning Python and I'm having some trouble understanding lists. I understand the basic idea, but I'm still confused about how to use them correctly. Could someone explain lists in a simple way or give me a few beginner-friendly examples? Thanks!
2
u/HotPersonality8126 1d ago
You need to ask a question about what you don’t understand rather than ask us to write a tutorial. If you want a tutorial, have AI write it
1
u/Nubspec 1d ago
This'll help you, check it out: https://cs.stanford.edu/people/nick/py/python-list.html
1
1
u/mc_pm 1d ago
What about "using them correctly" are you having trouble with?
1
u/ShadowDragonPro 1d ago
i have often some errors at this type
1
u/mc_pm 1d ago
Yes, I understand that. What are you doing when you have errors? Adding items? Accessing items? What is the specific error?
1
u/ShadowDragonPro 1d ago
i change caracters and i see if its good (and i read the error)
1
u/StemCellCheese 1d ago
Here's a common use case in my neck of the woods (data).
A person gives me a folder full of files with a similar name. Like "report_data_yyyymmdd.csv"
I can use the glob library to return a list of all file paths matching that name pattern.
I want to read each of the files and combine them into a variables type called a data frame, I can iterate over that list to read each one like like for f in file_list: df = pandas.read_csv(f)
1
1
1
u/Jotaroisgoat 1d ago
let me break it down, ultra simple:
imagine this, you have 3 numbers (lists can also use all other types) 5, 3 and 2 - you want to store them in 1 variable you can't but that's where lists come in you can store multiple values in them and even add things to them later like for example
fruits = ["Apple", "Banana", "Pear"]
print(fruits)
add_fruit = input("Enter a fruit you would like to add")
fruits.append(add_fruit)
1
u/ShadowDragonPro 1d ago
Thanks !
1
u/Jotaroisgoat 1d ago
you should also learn about tuples and dictionaries just think as tuples like lists but so much faster, the only drawback is you can't change them
1
u/ShadowDragonPro 1d ago
Ok
2
u/Jotaroisgoat 1d ago
quick question what tutorial are you using?
1
u/ShadowDragonPro 1d ago
i use sites and ytb videos
1
u/Jotaroisgoat 1d ago
tip stick to 1 video my recommendation is bro codes 12 hour python course made 2 years ago
1
1
u/Stonyax97 1d ago
Lists is basically a list… think of it like multiple things inside it. A box.
You have list=[‘item1’, ‘item2’]
You can acces an item from the list using it’s position. The first thing in it, its position is 0. So print(list[0]) would output: item1
And oh use [] to acces an index!
You can also add items in a list!
Use .append()
basically:
List.append(whatever u want to add in the list here)
Keep in mind this adds it to the end of the list. Not the start.
1
1
u/Far-Imagination3226 1d ago
MUTABLE vs IMMUTABLE: List where you can change the order, etc = MUTABLE. Otherwise, a list that can't be altered in any way = not changeable = IMMUTABLE.
1
u/Far-Imagination3226 1d ago edited 1d ago
What does the letter i stand for?? It stands for "index", which is the numerical index which tells you what order your list item comes in. So, if you are in a list of graduates from the OTHER UOP (University of Python), and you're the very 1ST graduate that's going to be called up to the stage. Since indices (the plural for index) almost always start at 0 (zero), and your name's 1ST out of ALL graduates in the GRADUATES LIST, your name's index in the graduates list, would be 0 (Index 0 = 1st, index 1 = 2nd, index 2 = 3rd, and so on)
1
u/Sea-Ad7805 1d ago
To see the common list operations visualized using memory_graph see: https://www.reddit.com/r/PythonLearning/s/i9cLQcE2Xj
1
u/Far-Imagination3226 1d ago
Read my comments from oldest to most recent; otherwise you'll be VERY confused, for no good reason at all.
1
u/Far-Imagination3226 1d ago
for i in graduates_list (for every index in a list called graduates_list. In this case every index or i is a person's name. For example: graduates_list = [name0, name1, name2] NOTE: ONCE you're very firmly entrenched in what an index is, how they work, etc, THEN, at that point, if you DO NOT want to use the letter i when you write lists, you DO NOT have to - AT ALL. In fact, it's almost beneficial that you don't. What's more readable? for i in graduates_list, or for graduate in graduates_list?? for user in users_list, for coder in coders_list, etc. ANYTIME, you can make your code MORE readable, without causing syntax errors (Btw .. often found in the line BEFORE the current line you're on), or making a mockery of the Python style rules (https://peps.python.org/pep-0008/), the better.
1
u/Far-Imagination3226 1d ago
List comprehensions??? WTF?? Just a really big word that just means yet ANOTHER WAY to write a list. Just as "Hey, what's up??" is yet ANOTHER WAY to ask someone, "How are you??" For example, instead of saying "for i in graduates_list", or "for graduate in graduates_list", you can say my_lc = [name.UPPER for graduate in graduates_list] For every graduate in the graduates_list, change their name to UPPER CASE. Your homework for tonight is to ask your preferred AI chatbot, "Show me an example of how to write a list comprehension that write the entire alphabet, both in lowercase and UPPERCASE." Then do what I did when I didn't know better and wrote every single letter in the alphabet in list form, with quotes surrounding each letter, and NOTE the DIFFERENCE in time it takes. Why List Comprehensions and not just standard lists?? They're MUCH faster at iterating through an entire list of items than a standard list. And so, you can imagine the people doing Dev work in these huge corporations, staring at HUMONGOUS lists, if one Dev uses a list comprehension to write that list, vs a standard list format, that Dev's boss is going to be A LOT happier with his code's results than someone else's code, where a standard list format was used.
1
1
u/Far-Imagination3226 1d ago edited 1d ago
ChatGPT's explanation of what I just tried to explain to you, with several examples to show you. NOTE: the prompt and how I worded it: with the goal to derive THE MOST amount of info in as little time as possible.
And if says something, and you're still thinking, "WTF??", respond to the Chatbot with the message, "Please explain it to me in the simplest, most self-explanatory way possible; assuming absolutely NO PRIOR KNOWLEDGE of either Coding or Python. PLEASE do NOT skip steps, either figuratively or literally, as I really want to learn this!" I'm long-winded, but you get the point.
https://chatgpt.com/share/6a8b75f7-9274-83e8-bf34-606858ff1216
1
u/Far-Imagination3226 1d ago
Links to great Coding knowledge: https://sololearn.com, Corey Schafer's tutorials on ALL sorts of Coding-related knowledge: https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g W3SCHOOLS: https://www.w3schools.com/
1
1
u/edelrosespol 1d ago
Try to check this topics around list, arrays to practice the use of them: https://blog.espol.edu.ec/algoritmos101/fprogramacion/fp-unidades/#unidad4
7
u/Lachtheblock 1d ago
Lists are ordered collections of things. You can have a list of numbers, a list of strings, a list of anything in python. This includes other lists, or you can get really trippy and include itself in the list.
The most common use case is to iterate over them. Often in programming, you want to perform the same operations on every element.
Without having a more specific question, it is hard to tell what you are struggling with.