{"id":2643,"date":"2023-10-16T15:35:57","date_gmt":"2023-10-16T10:05:57","guid":{"rendered":"https:\/\/python-programs.com\/?p=2643"},"modified":"2023-11-10T11:43:04","modified_gmt":"2023-11-10T06:13:04","slug":"python-how-to-iterate-over-a-list","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-iterate-over-a-list\/","title":{"rendered":"Python : How to Iterate over a List ?"},"content":{"rendered":"
Lists are similar to dynamically sized arrays, which are declared in other languages(e.g., vector in C++ and ArrayList in Java). Lists do not have to be homogeneous all of the time, which makes it a very useful tool in Python. DataTypes such as Integers, Strings, and Objects can all be included in a single list. Lists are mutable, which means they can be changed after they\u2019ve been created.<\/p>\n
We’ll look at a few different ways to iterate through a list in this article.<\/p>\n
Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to iterate through a list some of them are:<\/p>\n We can use for loop to iterate over the list.<\/p>\n Below is the implementation:<\/p>\n Output:<\/strong><\/p>\n In Python, the while loop is used to iterate through a block of code as long as the test expression (condition) is true. This loop is typically used when we don’t know how many times to iterate ahead of time.<\/p>\n Here the condition is loop till length of list<\/p>\n Below is the implementation:<\/p>\n Output:<\/strong><\/p>\n If we want to use the standard for loop, which iterates from x to y, we can do so.<\/p>\n Below is the implementation:<\/p>\n Output:<\/strong><\/p>\n We can iterate over the list in one line using list comprehension<\/p>\n Below is the implementation:<\/p>\n Output:<\/strong><\/p>\n The enumerate() function can be used to transform a list into an iterable list of tuples or to get the index based on a condition check.<\/p>\n Below is the implementation:<\/p>\n Output:<\/strong><\/p>\n Lists are similar to dynamically sized arrays, which are declared in other languages(e.g., vector in C++ and ArrayList in Java). Lists do not have to be homogeneous all of the time, which makes it a very useful tool in Python. DataTypes such as Integers, Strings, and Objects can all be included in a single list. …<\/p>\ngivenlist=['hello' , 'world' , 'this', 'is' ,'python']<\/pre>\n
hello \r\nworld\r\nthis \r\nis \r\npython<\/pre>\n
Print the list by iterating through it<\/h2>\n
\n
Method #1 : Using For loop<\/h3>\n
# Given list\r\ngivenlist=['hello' , 'world' , 'this', 'is' ,'python']\r\n\r\n#Using for loop to iterate over the list\r\nfor element in givenlist:\r\n print(element)\r\n<\/pre>\n
hello \r\nworld\r\nthis \r\nis \r\npython<\/pre>\n
Method #2 :Using while loop<\/h3>\n
# Given list\r\ngivenlist = ['hello', 'world', 'this', 'is', 'python']\r\n\r\n# Getting length of list\r\nlength = len(givenlist)\r\n\r\n# Initializing position to index 0\r\nposition = 0\r\n\r\n# Iterating using while loop\r\nwhile position < length:\r\n print(givenlist[position])\r\n position = position + 1\r\n<\/pre>\n
hello \r\nworld\r\nthis \r\nis \r\npython<\/pre>\n
Method #3: Using For loop and range() function<\/h3>\n
# Given list\r\ngivenlist = ['hello', 'world', 'this', 'is', 'python']\r\n\r\n# Getting length of list\r\nlength = len(givenlist)\r\n\r\n# Iterating till length of list\r\nfor index in range(length):\r\n print(givenlist[index])\r\n<\/pre>\n
hello \r\nworld\r\nthis \r\nis \r\npython<\/pre>\n
Method #4 : Using list comprehension<\/h3>\n
# Given list\r\ngivenlist = ['hello', 'world', 'this', 'is', 'python']\r\n\r\n# Using list comprehension\r\n[print(i) for i in givenlist]\r\n<\/pre>\n
hello \r\nworld\r\nthis \r\nis \r\npython<\/pre>\n
Method #5 :Using enumerate()<\/h3>\n
# Given list\r\ngivenlist = ['hello', 'world', 'this', 'is', 'python']\r\n\r\n# Using enumerate()\r\nfor index, value in enumerate(givenlist):\r\n print(value)\r\n<\/pre>\n
hello \r\nworld\r\nthis \r\nis \r\npython<\/pre>\n
\nRelated Programs<\/strong>:<\/p>\n\n