Learn about the two mains ways to iterate over each item in a Python list: "the for item in ..." syntax and the "for i in range(...)" syntax.
- [Narrator] Now let's quickly go over two different ways to iterate over each element in a list in Python. Let's say we have our list a with the elements item zero, item one and item two. The first way to iterate over each element in this list is this, for element in a, print element and this would go through each item in this list and print it out. Item zero, item one and item two.
So that's the first method. To understand the second way to iterate over each element in a list, you first need to understand what the range function does. Range of three produces essentially a list with the elements zero, one and two. It's not exactly a list but it acts like it in this four loop segment. For i in range, three, print i. This would print out zero, one and two because we are just iterating over each element in this like-like object with the elements zero, one and two.
Using this we can now iterate over each element in any list by index. Let's say we have the same list as before. A we did elements, item zero, item one, and item two. So if we do four i in range of three, print a[i], i would go from zero to one to two and this block will print out item zero, item one and item two because a returns item zero, and a returns item one and a returns item two.
And instead of writing for i in range of three, we can also replace three with len(a) because the length of this list is three. These two methods of list iteration are useful in different situations. For example, the for i in range syntax allows you to iterate over multiple lists at the same time using the same index.
Follow YK Sugishita as he explains what to keep in mind as you prepare for coding interviews. YK covers useful concepts that can help you master your interview, followed by practice problems that test what you know. YK reviews key concepts such as two-dimensional arrays, time complexity, Big-O notation, and hash tables. To wrap up, he shares a few tips for acing your interview, such as how to come up with an optimal solution. Even if you're not prepping for an upcoming job interview, tackling the coding problems covered in this course is a great way to sharpen your programming skills.
- Honing your problem-solving skills
- Preparing for a coding interview
- Using Jupyter Notebook for sample problems in Python
- Using arrays to solve interview problems
- Working with and comparing strings
- Using two-dimensional arrays
- Learning the importance of time complexity in code
- Using Big-O notation to describe the time complexity of code
- Using hash tables and Python dictionaries in Python
- Asking clarifying questions during your interview
- Finding the most efficient solutions to problems
- Testing your proposed code solutions