List of Delimited Strings¶
Let us get into the details about list of delimited strings.
- Here is an example of a delimited string –
'1,ktrett0@independent.co.uk,6998.95'
. - Delimiter is a synonym for Separator.
- It has values related to 3 attributes – employee id, email and salary.
- Comma (
,
) is the delimiter that is used in this string. Hence, it is a comma delimited string. It is also known as comma separated string. - We typically have to deal with list of elements like the one above. It is generally known as list of comma separated strings.
- Here is an example for list of comma separated strings related to employees.
In [2]:
employees = [
'1,ktrett0@independent.co.uk,6998.95',
'2,khaddock1@deviantart.com,10572.4',
'3,ecraft2@dell.com,3967.35',
'4,drussam3@t-online.de,17672.44',
'5,graigatt4@github.io,11660.67',
'6,bjaxon5@salon.com,18614.93',
'7,araulston6@list-manage.com,11550.75',
'8,mcobb7@mozilla.com,17016.15',
'9,grobardley8@unesco.org,14141.25',
'10,bbuye9@vkontakte.ru,12193.2'
]
- The type of employees is
list
. You can runtype(employees)
to get the type of employees.
In [3]:
type(employees)
Out[3]:
list
- You can check the data type of each element in the list by running below code.
In [4]:
for employee in employees:
print(f'The data type of {employee} is {type(employee)}')
The data type of 1,ktrett0@independent.co.uk,6998.95 is <class 'str'> The data type of 2,khaddock1@deviantart.com,10572.4 is <class 'str'> The data type of 3,ecraft2@dell.com,3967.35 is <class 'str'> The data type of 4,drussam3@t-online.de,17672.44 is <class 'str'> The data type of 5,graigatt4@github.io,11660.67 is <class 'str'> The data type of 6,bjaxon5@salon.com,18614.93 is <class 'str'> The data type of 7,araulston6@list-manage.com,11550.75 is <class 'str'> The data type of 8,mcobb7@mozilla.com,17016.15 is <class 'str'> The data type of 9,grobardley8@unesco.org,14141.25 is <class 'str'> The data type of 10,bbuye9@vkontakte.ru,12193.2 is <class 'str'>
- We can apply string manipulation functions to get the information we are looking for from the original strings that are part of the lists.
- Let us extract list of employee email from employees list. We will deep dive about how to process these list of strings as part of topics related to manipulating collections very soon.
In [5]:
employees[0]
Out[5]:
'1,ktrett0@independent.co.uk,6998.95'
In [6]:
type(employees[0])
Out[6]:
str
In [7]:
employees[0].split(',')
Out[7]:
['1', 'ktrett0@independent.co.uk', '6998.95']
In [8]:
employees[0].split(',')[1]
Out[8]:
'ktrett0@independent.co.uk'
In [9]:
for employee in employees:
print(employee.split(',')[1])
ktrett0@independent.co.uk khaddock1@deviantart.com ecraft2@dell.com drussam3@t-online.de graigatt4@github.io bjaxon5@salon.com araulston6@list-manage.com mcobb7@mozilla.com grobardley8@unesco.org bbuye9@vkontakte.ru