# Anyone knows python..?

**URL:** <https://onehack.st/t/anyone-knows-python/178746>\
**Category:** Discussion & Solutions\
**Tags:** solved\
**Created:** [April 14, 2021, 11:24am UTC](https://onehack.st/t/anyone-knows-python/178746 "2021-04-14T11:24:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aadii](https://onehack.st/user_avatar/onehack.st/aadii/32/19131_2.png) [@Aadii](https://onehack.st/u/Aadii)\
**Post date:** [April 14, 2021, 11:24am UTC](https://onehack.st/t/anyone-knows-python/178746/1 "2021-04-14T11:24:46Z")

</div>

Hey folks,  
I need help with my python code  
Here is the list:-  
list=[[“Doe”,1,74.0],[“John”,2,65.0],[“Lorem”,3,99.0]]

I want to delete the whole sublist with an index of the middle number  
for eg. I’ll give input as 2 and delete item should be [“John”,2,65.0].  
so list will be  
list=[[“doe”,1,74.0],[“Lorem”,3,99.0]].

Thanks in advance

---

<div class="post-metadata">

**Author:** ![astrid01](https://onehack.st/user_avatar/onehack.st/astrid01/32/87183_2.png) [@astrid01](https://onehack.st/u/astrid01)\
**Post date:** [April 14, 2021, 11:29am UTC](https://onehack.st/t/anyone-knows-python/178746/2 "2021-04-14T11:29:39Z")

</div>

> **[W3Schools.com](https://www.w3schools.com/python/ref_list_pop.asp)**
>
> W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

list.pop(1)  
Index starts from 0.  
So to delete 2nd sublist, use index 1.

---

<div class="post-metadata">

**Author:** ![Andrei\_Vlad](https://onehack.st/user_avatar/onehack.st/andrei_vlad/32/11800_2.png) [@Andrei\_Vlad](https://onehack.st/u/Andrei_Vlad)\
**Post date:** [April 14, 2021, 11:36am UTC](https://onehack.st/t/anyone-knows-python/178746/3 "2021-04-14T11:36:44Z")

</div>

Good work @astrid01 and good of you to share the link for the op to reference it later 👍

---

<div class="post-metadata">

**Author:** ![Saurav\_P](https://onehack.st/user_avatar/onehack.st/saurav_p/32/24576_2.png) [@Saurav\_P](https://onehack.st/u/Saurav_P)\
**Post date:** [April 15, 2021, 4:48pm UTC](https://onehack.st/t/anyone-knows-python/178746/5 "2021-04-15T16:48:48Z")

</div>

Just wanted to know can we delete any element of a sublist according to the input  
like: I enter 2 and 1 so it should remove Lorem from the 3rd sublist

---

<div class="post-metadata">

**Author:** ![astrid01](https://onehack.st/user_avatar/onehack.st/astrid01/32/87183_2.png) [@astrid01](https://onehack.st/u/astrid01)\
**Post date:** [April 15, 2021, 5:57pm UTC](https://onehack.st/t/anyone-knows-python/178746/6 "2021-04-15T17:57:34Z")

</div>

Yes you can.

# list[element].pop(nested\_element)

list[2].pop(1)

---

<div class="post-metadata">

**Author:** ![Edward\_Praveen](https://onehack.st/user_avatar/onehack.st/edward_praveen/32/20420_2.png) [@Edward\_Praveen](https://onehack.st/u/Edward_Praveen)\
**Post date:** [April 15, 2021, 6:03pm UTC](https://onehack.st/t/anyone-knows-python/178746/7 "2021-04-15T18:03:35Z")

</div>

```
a=[['Doe',1,74.0],['John',2,65.0],['Lorem',3,99.0]]

print(a[2].pop(0)

print(a) #gives [['Doe', 1, 74.0], ['John', 2, 65.0], [3, 99.0]]

```

---

<div class="post-metadata">

**Author:** ![La\_Signora](https://onehack.st/user_avatar/onehack.st/la_signora/32/115755_2.png) [@La\_Signora](https://onehack.st/u/La_Signora)\
**Post date:** [April 17, 2021, 9:24am UTC](https://onehack.st/t/anyone-knows-python/178746/8 "2021-04-17T09:24:20Z")

</div>

Just do it like

```python
try: # because the input may go beyond the bounds
    del list[int(input())] 
    # or list.pop(int(input()) 
    # or list=[list[i] for i in range(len(list)) if i != int(input())]
except:
    print('The index you entered is out of bounds')
```

---

<div class="post-metadata">

**Author:** ![La\_Signora](https://onehack.st/user_avatar/onehack.st/la_signora/32/115755_2.png) [@La\_Signora](https://onehack.st/u/La_Signora)\
**Post date:** [April 15, 2021, 3:41pm UTC](https://onehack.st/t/anyone-knows-python/178746/4 "2021-04-15T15:41:15Z")

</div>

There are many ways to do this.

```python
a=[['Doe',1,74.0],['John',2,65.0],['Lorem',3,99.0]]

```

## Method 1:

```python
del a[1]

```

[Try it online!](https://tio.run/##K6gsycjPM/7/P9E2OlrdJT9VXcdQx9xEzyBWJ1rdKz8jT13HSMfMFML3yS9KzVXXMdaxtAQKxHKlpOYoJEYbxnIVFGXmlWgkav7/DwA "Python 3 – Try It Online")

## Method 2:

```python3
a.pop(1)

```

[Try it online!](https://tio.run/##K6gsycjPM/7/P9E2OlrdJT9VXcdQx9xEzyBWJ1rdKz8jT13HSMfMFML3yS9KzVXXMdaxtAQKxHIl6hXkF2gYanIVFGXmlWgkav7/DwA "Python 3 – Try It Online")

## Method 3:

```python3
a=[a[i] for i in range(len(a)) if i != 1]

```

[Try it online!](https://tio.run/##K6gsycjPM/7/P9E2OlrdJT9VXcdQx9xEzyBWJ1rdKz8jT13HSMfMFML3yS9KzVXXMdaxtAQKxHIB9SRGZ8YqpOUXKWQqZOYpFCXmpadq5KTmaSRqaipkpgFFFW0VDGO5Cooy80qAgv//AwA "Python 3 – Try It Online")

[Next page](https://onehack.st/t/anyone-knows-python/178746.md?page=2)
