1
00:00:00,640 --> 00:00:01,420
Hi, everyone.

2
00:00:01,420 --> 00:00:08,530
In today's example, we're going to transform this list of file names into this.

3
00:00:08,920 --> 00:00:10,990
So that's the output of the program.

4
00:00:12,380 --> 00:00:18,050
So let's suppose we had some files and we got their file names using some other functions, which we'll

5
00:00:18,050 --> 00:00:19,550
learn later on in the course.

6
00:00:19,550 --> 00:00:22,340
And those functions created a list.

7
00:00:22,340 --> 00:00:30,470
And then you want to first change these strings into the new names.

8
00:00:31,700 --> 00:00:35,390
And for that you use a list comprehension, which we will do now.

9
00:00:35,390 --> 00:00:42,920
And then maybe later on, you reassign those strings to the existing files on disk.

10
00:00:44,420 --> 00:00:49,460
So today we're simply going to work on transforming the strings.

11
00:00:51,730 --> 00:00:55,660
So we got these names, the existing names of the files.

12
00:00:57,810 --> 00:00:59,720
Now we want to transform them.

13
00:00:59,730 --> 00:01:05,880
So when you want to transform a list, what you think is a list comprehension.

14
00:01:06,300 --> 00:01:08,370
So let's write the list comprehension.

15
00:01:08,850 --> 00:01:15,720
We will replace we will overwrite the existing variable with the new list, which is constructed by

16
00:01:15,720 --> 00:01:17,010
the list comprehension.

17
00:01:19,160 --> 00:01:21,910
So what do we want to do to the file names?

18
00:01:21,920 --> 00:01:30,770
You see, this is one dot, doc, and we want to transform it to one dash doc plus dot text.

19
00:01:33,050 --> 00:01:36,950
So how do we replace the dots with a dash?

20
00:01:37,280 --> 00:01:40,940
Well, using that replace.

21
00:01:41,180 --> 00:01:43,160
So file name is a string.

22
00:01:43,910 --> 00:01:46,730
In the first iteration, it represents the first string.

23
00:01:46,820 --> 00:01:53,510
So that expects a string, which means the character you want to replace.

24
00:01:53,510 --> 00:01:59,420
And another string, the character that you want the existing character to be replaced with.

25
00:02:02,670 --> 00:02:05,670
So the new list is going to contain.

26
00:02:07,170 --> 00:02:15,300
The string with that character replaced, plus another string, which is the text.

27
00:02:16,900 --> 00:02:19,330
So that will construct a new string.

28
00:02:21,020 --> 00:02:23,300
Because it is possible to.

29
00:02:28,810 --> 00:02:31,630
Concatenate two strings so you get.

30
00:02:31,630 --> 00:02:32,470
Hey there.

31
00:02:32,920 --> 00:02:34,300
That's what we're doing there.

32
00:02:36,160 --> 00:02:44,380
For file name in file names, print, file names, run.

33
00:02:44,650 --> 00:02:46,750
And here is the result.

34
00:02:48,130 --> 00:02:49,870
So that's the final code.

35
00:02:52,600 --> 00:02:55,370
That is the syntax of the list comprehension.

36
00:02:55,380 --> 00:03:00,740
So it always starts with the new version of each item.

37
00:03:00,750 --> 00:03:04,530
So the new version of each item is this.

38
00:03:06,130 --> 00:03:07,590
The character replaced.

39
00:03:07,600 --> 00:03:10,330
Plus this added to the.

40
00:03:11,430 --> 00:03:12,960
New string.

41
00:03:15,100 --> 00:03:19,270
And you do that for each file name in the file names list.

42
00:03:19,900 --> 00:03:21,100
So thanks for following.

