Nov-15-2019, 02:43 PM
I’m taking Colt Steele’s Udemy course called “The Modern Python 3 Bootcamp”. I’m on Section 13 on List Comprehension.
The instructor uses this as an example:
['N', 'E', 'B', 'U', 'C', 'H', 'A', 'D', 'N', 'E', 'Z', 'Z', 'A', 'R']
In my pseudo code that I came up with, these two lines could be read as: "Give me the uppercase format of every character for every character inside the
Is that pseudo code correct?
I am now taking the above list comprehension explanation a step further by trying to do something the course instructor did not do, which is re-write the simple algorithm as a regular for loop. Here is my valiant attempt:
The output is just
I’m not sure why or what I am doing wrong. I tried adding the append method to line 5 but that threw a syntax error. Would someone here be able to rewrite the original list comprehension as a regular for loop?
Here are some of the resources I have already consulted:
By the way, more of my list-comprehension practice that I have done today can be found here in a Jupyter Notebook in my GitHub repo.
The instructor uses this as an example:
name = 'Nebuchadnezzar' print([char.upper() for char in name])Here is the output:
['N', 'E', 'B', 'U', 'C', 'H', 'A', 'D', 'N', 'E', 'Z', 'Z', 'A', 'R']
In my pseudo code that I came up with, these two lines could be read as: "Give me the uppercase format of every character for every character inside the
name
variable (which is a string) containing: 'Nebuchadnezzar'."Is that pseudo code correct?
I am now taking the above list comprehension explanation a step further by trying to do something the course instructor did not do, which is re-write the simple algorithm as a regular for loop. Here is my valiant attempt:
name = 'Nebuchadnezzar' new_name = [] for char in name: new_name = char.upper() char + char print(new_name)
The output is just
R
. I was expecting: ['N', 'E', 'B', 'U', 'C', 'H', 'A', 'D', 'N', 'E', 'Z', 'Z', 'A', 'R']I’m not sure why or what I am doing wrong. I tried adding the append method to line 5 but that threw a syntax error. Would someone here be able to rewrite the original list comprehension as a regular for loop?
Here are some of the resources I have already consulted:
- "How to create a list of random integers in python?"
- My old thread titled: "Creating list out of the first letter of every word in a string" (but @volcano63’s post specifically)
- "question about list comprehension"
- "List Comprehensions in Python"
- "Python List Comprehension"
By the way, more of my list-comprehension practice that I have done today can be found here in a Jupyter Notebook in my GitHub repo.