for word in info:
is a loop. Its body [the indented part] will execute once for each element of info
.
You could access info
elements by index, as other answers suggest. You can also unpack the list into reasonably-named variables:
name, job, marital_status = info # the unpacking: 3 variables for 3 list items
print "My name is", name, "my job is", job, "I am", marital_status
The loop comes in handy when you have a list of people to process:
people = [ # a list of lists
["Joe", "painter", "single"],
["Jane", "writer", "divorced"],
["Mario", "plumber", "married"]
]
for person_info in people:
print "Name:", person_info[0], "job:", person_info[1], "is", person_info[2]
It is idiomatic to unpack nested items right in the loop:
for name, job, marital_status in people:
print "Person named", name, "works as a", job, "and is", marital_status
We sometimes come through situations where we require to get all the words present in the string, this can be a tedious task done using the native method. Hence having shorthands to perform this task is always useful. Additionally, this article also includes the cases in which punctuation marks have to be ignored.
Method #1 : Using split[]
Using the split function, we can split the string into a list of words and this is the most generic and recommended
method if one wished to accomplish this particular task. But the drawback is that it fails in cases the string contains punctuation marks.
Python3
test_string
=
"Geeksforgeeks is best Computer Science Portal"
print
[
"The original string is : "
+
test_string]
res
=
test_string.split[]
print
[
"The list of words is : "
+
str
[res]]
Output:
The original string is : Geeksforgeeks is best Computer Science
Portal
The list of words is : [‘Geeksforgeeks’, ‘is’, ‘best’, ‘Computer’, ‘Science’, ‘Portal’]
Method #2 : Using regex[ findall[] ]
In the cases which contain all the special characters and punctuation marks, as discussed above, the conventional method of finding words in string using split can fail and hence requires regular expressions to perform this task. findall function returns the list after filtering the string and
extracting words ignoring punctuation marks.
Python3
import
re
test_string
=
"Geeksforgeeks, is best @# Computer Science Portal.!!!"
print
[
"The original string is : "
+
test_string]
res
=
re.findall[r
'\w+'
, test_string]
print
[
"The list of words is : "
+
str
[res]]
Output:
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The list of words is :
[‘Geeksforgeeks’, ‘is’, ‘best’, ‘Computer’, ‘Science’, ‘Portal’]
Method #3 : Using regex[] + string.punctuation
This method also used regular expressions, but string function of getting all the punctuations is used to ignore all the punctuation marks and get the filtered result string.
Python3
import
re
import
string
test_string
=
"Geeksforgeeks, is best @# Computer Science Portal.!!!"
print
[
"The original string is : "
+
test_string]
res
=
re.sub[
'['
+
string.punctuation
+
']'
, '', test_string].split[]
print
[
"The list of words is : "
+
str
[res]]
Output:
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The list of words is : [‘Geeksforgeeks’, ‘is’, ‘best’, ‘Computer’, ‘Science’, ‘Portal’]
When it is required to extract keywords from a list, a simple iteration and the ‘iskeyword’ method is used.
Example
Below is a demonstration of the same −
import keyword my_list = ["python", 'is', 'fun', 'to', 'learn'] print["The list is :"] print[my_list] my_result = [] for element in my_list: for word in element.split[]: if keyword.iskeyword[word]: my_result.append[word] print["The result is :"] print[my_result]
Output
The list is : ['python', 'is', 'fun', 'to', 'learn'] The result is : ['is']
Explanation
A list of strings is defined and is displayed on the console.
An empty list is defined.
The list is iterated over, and every element is split based on spaces.
The ‘iskeyword’ method is used to check if any of the elements in the list are a keyword in the language.
If yes, it is appended to the empty list.
This list is displayed on the console as the output.
Updated on 08-Sep-2021 07:09:22
- Related Questions & Answers
- Python Program to Extract Elements from a List in a Set
- Python program to extract characters in given range from a string list
- Python Program that extract words starting with Vowel From A list
- Extract digits from Tuple list Python
- List of Keywords in Python Programming
- Python program to extract only the numbers from a list which have some specific digits
- Python – Extract element from a list succeeded by K
- Extract numbers from list of strings in Python
- Python – Extract elements from Ranges in List
- Python Program to Extract Strings with at least given number of characters from other list
- How to extract first value from a list in R?
- Python program to extract ‘k’ bits from a given position?
- Python program to mask a list using values from another list
- Python Program to extract email-id from URL text file
- Python program to remove Duplicates elements from a List?