# Random.sample(list, len(list))

**URL:** <https://community.deeplearning.ai/t/random-sample-list-len-list/173485>\
**Category:** Convolutional Neural Networks in TensorFlow\
**Tags:** week-module-1\
**Created:** [July 30, 2022, 7:11am UTC](https://community.deeplearning.ai/t/random-sample-list-len-list/173485 "2022-07-30T07:11:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sajjad\_soltani1](https://avatars.discourse-cdn.com/v4/letter/s/9dc877/32.png) [@sajjad\_soltani1](https://community.deeplearning.ai/u/sajjad_soltani1)\
**Post date:** [July 30, 2022, 7:11am UTC](https://community.deeplearning.ai/t/random-sample-list-len-list/173485/1 "2022-07-30T07:11:01Z")

</div>

it seems that we cant shuffle items of a directory with random.sample . because we should write : a=random.sample(source\_directory, len(source\_directory))  
after this we need to split data so we write :

```
        for file in os.scandir(a):

                size=os.path.getsize(file)

                if size == 0 :
   
                     print(f"{file.name} is zero length, so ignoring.")

                     os.remove(file.path)

```

but it gives eror about the for loop  
how we can shuffle items in directory?  
]

---

<div class="post-metadata">

**Author:** ![balaji.ambresh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/balaji.ambresh/32/128697_2.png) [@balaji.ambresh](https://community.deeplearning.ai/u/balaji.ambresh)\
**Post date:** [July 30, 2022, 7:15am UTC](https://community.deeplearning.ai/t/random-sample-list-len-list/173485/2 "2022-07-30T07:15:14Z")

</div>

You are right in pointing out that we can’t shuffle items of a directory. But, this is not what the assignment asks you to do.  
Please do this:

1. List contents of the source directory. See `os.listdir`
2. Create a filtered list of files whose size is not 0 bytes.
3. Shuffle the filtered list of path / file names.

---

<div class="post-metadata">

**Author:** ![sajjad\_soltani1](https://avatars.discourse-cdn.com/v4/letter/s/9dc877/32.png) [@sajjad\_soltani1](https://community.deeplearning.ai/u/sajjad_soltani1)\
**Post date:** [August 1, 2022, 6:00am UTC](https://community.deeplearning.ai/t/random-sample-list-len-list/173485/3 "2022-08-01T06:00:41Z")

</div>

you mean that i have to put random.sample in the loop which filters the size of images?

---

<div class="post-metadata">

**Author:** ![balaji.ambresh](https://sea1.discourse-cdn.com/dlai/user_avatar/community.deeplearning.ai/balaji.ambresh/32/128697_2.png) [@balaji.ambresh](https://community.deeplearning.ai/u/balaji.ambresh)\
**Post date:** [August 1, 2022, 11:42am UTC](https://community.deeplearning.ai/t/random-sample-list-len-list/173485/4 "2022-08-01T11:42:27Z")

</div>

You have to sample valid images. So, use `random.sample` outside the loop.
